I have to following query in SQL Server:
SELECT EmployeeID,
TotalQuantity AS TotalQty,
TotalSales,
MAX(CASE WHEN MonthNumber = MAX(MonthNumber)
THEN TotalSales END) as RecentMonthSale
FROM vwSales
GROUP BY EmployeeID, TotalQuantity , TotalSales
Bu it gives me the error:
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
Input View is as follows:
EmployeeID TotaSales MonthNumber
1 4000 1
1 6000 2
2 8500 1
2 6081 2
Desired output:
EmployeeID TotalSale RecentMonthSale
1 10000 6000
2 14581 6081
3 11458 1012
I want following column in my output EmployeeID, TotalQuantity TotalSale RecentMonthSale My View has the following column EmployeeID TotalSale,TotalQuantity, MonthNumber.
This query will show the output that you need, and will scan the table only one time.