Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
karthickbabu
Posting Yak Master
151 Posts |
Posted - 2008-01-14 : 01:24:55
|
| I wrote the query using pivot operator but its error'Incorrect Syntax near ( 'Can any one find this, My code as belowCREATE TABLE #Sales( OrderYear DATETIME NOT NULL, SalesPersonID int NOT NULL, Total money NOT NULL)GOINSERT INTO #Sales SELECT '4/23/2005' ,230, 4500INSERT INTO #Sales SELECT '12/2/2005' ,240, 5000INSERT INTO #Sales SELECT '11/15/2006' ,230, 6000INSERT INTO #Sales SELECT '9/6/2006' ,240, 2000INSERT INTO #Sales SELECT '7/25/2005' ,250, 8000INSERT INTO #Sales SELECT '6/20/2007' ,230, 6500INSERT INTO #Sales SELECT '1/31/2007' ,250, 5000GoSELECT * FROM #SalesSELECT SalesPersonID, SUM(CASE when YEAR(OrderDate) = 2005 then Total ELSE 0 end) as '2005', SUM(CASE when YEAR(OrderDate) = 2006 then Total ELSE 0 end) as '2006', SUM(CASE when YEAR(OrderDate) = 2007 then Total ELSE 0 end) as '2007'FROM #SalesGROUP BY SalesPersonIDSELECT SalesPersonID, [2005], [2006], [2007] FROM #Sales PIVOT (SUM(Total) FOR YEAR(OrderDate) IN ([2005],[2006],[2007])) AS SalesPivot ORDER BY SalesPersonIDIs it possible to use 'YEAR(OrderDate)' In (SUM(Total) FOR YEAR(OrderDate) IN ([2005],[2006],[2007]))Kindly reply |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-01-14 : 03:20:02
|
| SELECT SalesPersonID, [2005], [2006], [2007] FROM (select SalesPersonID, Total,year(orderyear) as orderyear from #Sales) pPIVOT (SUM(Total) FOR Orderyear IN ([2005],[2006],[2007]))AS SalesPivot ORDER BY SalesPersonIDMadhivananFailing to plan is Planning to fail |
 |
|
|
karthickbabu
Posting Yak Master
151 Posts |
Posted - 2008-01-14 : 03:45:58
|
| Thank you what that p refers, Is it object for pivot keyword or someother purpose |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-01-14 : 03:51:45
|
it is a table alias for the derived table KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-01-14 : 05:38:45
|
quote: Originally posted by karthickbabu Thank you what that p refers, Is it object for pivot keyword or someother purpose
Also read the query again. I also used derived tableMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|