Friday, July 5, 2013

Pivot Example



SELECT * FROM Sales



SELECT EmpId, [2005], [2006], [2007]

FROM (SELECT EmpId, Yr, Sales FROM Sales) AS s

PIVOT (SUM(Sales) FOR Yr IN ([2005], [2006], [2007])) AS p

Output

EmpId
Yr
Sales
1
2005
12000
1
2006
18000
1
2007
25000
2
2005
15000
2
2006
6000
3
2006
20000
3
2007
24000



EmpId
2005
2006
2007
1
12000
18000
25000
2
15000
6000
NULL
3
NULL
20000
24000

No comments:

Post a Comment