I want to Group BY Date and Payment and branch name and code I don't know why this SQL query return this error:
Column 'FactSalesTable.SalesTotal' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
SELECT
FactSalesTable.Date,
FactSalesTable.PaymentID,
Branch.* ,
SUM(FactSalesTable.SalesTotal) AS TotalSales,
AVG (FactSalesTable.SalesTotal) OVER (PARTITION BY Branch.BCode) AS AvgSales,
CASE
WHEN SUM(FactSalesTable.SalesTotal) OVER (PARTITION BY Branch.BCode) /AVG(FactSalesTable.SalesTotal) OVER (PARTITION BY Branch.BCode) > 1 THEN 'Above Avg'
WHEN SUM(FactSalesTable.SalesTotal) OVER (PARTITION BY Branch.BCode)/AVG(FactSalesTable.SalesTotal) OVER (PARTITION BY Branch.BCode) = 1 THEN 'Avg'
ELSE 'Below Avg'
END AS SalesResult
FROM Branch
LEFT JOIN FactSalesTable ON Branch.BCode = FactSalesTable.BCode
WHERE FactSalesTable.SalesTotal > 0 AND Branch.BCode = 1021
GROUP BY FactSalesTable.Date, Branch.BranchName, Branch.BCode, FactSalesTable.PaymentID
ORDER BY FactSalesTable.Date DESC
I want to show the total sales by date like
date | BCode | total sales | avg |
---|---|---|---|
2023-04-30 | 1021 | 4333 | 5050 |
2022-04-30 | 1021 | 6392 | 5050 |
2022-04-29 | 1021 | 3544 | 5050 |
2022-04-29 | 1021 | 2300 | 5050 |
This is a snip of the query without the group by
You are aggregating at two levels:
SUM(SalesTotal)
for each branch & payment as defined by the GROUP BY
.SUM(...) OVER(...)
and AVG(...) OVER(...)
across all payments per branch as defined by the OVER(,,,)
window specifier.Your problem is that your windowed aggregation functions are attempting to access the raw data, when I believe you intend to sum and average the grouped-by values. Instead of AVG(SalesTotal) OVER(...)
, I believe you need AVG(SUM(SalesTotal)) OVER(...)
. However, for the SUM()
cases in the SalesResult
calculatiion, I believe that you want just the plain (non-windowed) branch & payment sum SUM(SalesTotal)
, not the sum of sums at the branch level.
Also, sinve you have constrained FactSalesTable.SalesTotal > 0
, the FactSalesTable
rows must be present and your LEFT JOIN
is effectively an INNER JOIN
.
The following should be closer to what you are trying to achieve.
SELECT
FactSalesTable.Date,
FactSalesTable.PaymentID,
Branch.* ,
SUM(FactSalesTable.SalesTotal) AS TotalSales,
AVG (SUM(FactSalesTable.SalesTotal)) OVER(PARTITION BY Branch.BCode) as AvgSales ,
case
when SUM(FactSalesTable.SalesTotal)
/ AVG(SUM(FactSalesTable.SalesTotal)) OVER(PARTITION BY Branch.BCode) > 1
THEN 'Above Avg'
when SUM(FactSalesTable.SalesTotal)
/ AVG(SUM(FactSalesTable.SalesTotal)) OVER(PARTITION BY Branch.BCode) = 1
THEN 'Avg'
else 'Below Avg'
end as SalesResult
FROM Branch
LEFT JOIN FactSalesTable ON Branch.BCode = FactSalesTable.BCode
WHERE FactSalesTable.SalesTotal > 0 and Branch.BCode = 1021
Group BY FactSalesTable.Date,Branch.BranchName ,Branch.BCode,FactSalesTable.PaymentID
ORDER BY FactSalesTable.Date DESC
Results:
Date | PaymentID | BCode | BranchName | TotalSales | AvgSales | SalesResult |
---|---|---|---|---|---|---|
2022-04-30 | 57517 | 1021 | Main Branch | 19939.00 | 16223.000000 | Above Avg |
2022-04-30 | 57519 | 1021 | Main Branch | 12507.00 | 16223.000000 | Below Avg |
See this db<>fiddle. (Thanks to Bart McEndree for the test data setup.)
There are likely ways to further restructure the query to limit redundant expressions and improve readability, such as my moving the initial grouping logic to a subquery or CTE, and performing the windowed operations in the final/outer query. Another might be to wrap all payment logic up in a CROSS APPLY
.