I want to show the year as column and salesamount below it.
This query shows OrderYear and then subtotal:
Select YEAR(OrderDate) As OrderYear, SUM(SubTotal) as OrderTotal
From Sales.SalesOrderHeader s
Group by YEAR(OrderDate)
So I had to write a pivot query for it. This is my pivot query which is not working:
Select [2001] , [2002], [2003], [2004]
From
(
Select YEAR(OrderDate) As OrderYear, SUM(SubTotal) as OrderTotal
From Sales.SalesOrderHeader s
Group by YEAR(OrderDate)
) a
Pivot(
OrderTotal FOR OrderYear In ([2001] , [2002], [2003], [2004])
) pvt
Can anybody spot the mistake? I am using AdventureWorks sample database.
You are missing an aggregate.
This is still required by the syntax even if you only expect there to be only one value ever being aggregated (in which case either
MAXorMINwill do)Though actually in your case you don’t need the inner aggregation and can use