A PIVOT rotates a table-valued expression by turning the unique values from one column in the expression into multiple columns in the output, and performs aggregations where they are required on any remaining column values that are wanted in the final output.
Example 1
Table One
Id Area Result
1 A Pass
2 B Fail
3 A Pass
4 B Pass
5 A Fail
- SELECT * FROM dbo.Result
- PIVOT ( count(Id) FOR Result in (Pass, Fail) ) AS RESULT
Area Pass Fail
A 2 1
B 1 1
Note: the "select * from result" will contain id, area and result columns, but it will display only an area because the id and result columns are used in a pivot, in other words count (id) and for result. So the select statement will display area, pass and fail columns.
- SELECT * FROM dbo.Result
- PIVOT ( count(Id) FOR Result in (Pass, Fail, Promoted) ) AS RESULT
Area Pass Fail Promoted
A 2 1 0
B 1 1 0
- SELECT * FROM dbo.Result
- PIVOT ( count(Area) FOR Result in (Pass, Fail) ) AS RESULT
Id Pass Fail
1 1 0
2 0 1
3 1 0
4 1 0
5 0 1
Example 2
Sales Table
Month SaleAmount
January 100
February 200
March 300
- SELECT * FROM SALES
- PIVOT ( SUM(SaleAmount) FOR Month IN (January, February, March)) AS A
- SELECT [January]
- , [February]
- , [March]
- FROM ( SELECT [Month]
- , SaleAmount
- FROM Sales
- ) p PIVOT ( SUM(SaleAmount)
- FOR [Month]
- IN ([January],[February],[March])
- ) AS pvt
January February March
100 200 300
- SELECT * FROM SALES
- PIVOT ( COUNT(SaleAmount) FOR Month IN (January, February, March)) AS A
January February March
1 1 1
- SELECT * FROM SALES
- PIVOT ( count(Month) FOR Month IN (January, February, March)) AS A
SaleAmount January February March
100 1 0 0
200 0 1 0
300 0 0 1
Example 3
T1 Table
No ID Date Value
1 1001 2009-05-01 00:00:00.000 101.00
1 1001 2009-05-15 00:00:00.000 102.00
1 1001 2009-05-20 00:00:00.000 105.00
2 1001 2009-05-01 00:00:00.000 41.00
2 1001 2009-05-15 00:00:00.000 44.00
3 1001 2009-06-01 00:00:00.000 330.00
- SELECT * FROM T1
- PIVOT ( SUM(Value) FOR Date in ([05/01/2009] ,[05/15/2009] ,[05/20/2009] ,[06/01/2009]) ) as a
- SELECT [No] , [ID] ,[05/01/2009] ,[05/15/2009] ,[05/20/2009] ,[06/01/2009]
- FROM (
- SELECT [No], [ID], [Date], [Value]
- FROM T1) up
- PIVOT ( sum([Value]) FOR [Date] in ([05/01/2009] ,[05/15/2009] ,[05/20/2009] ,[06/01/2009]) )AS pvt
No ID 05/01/2009 05/15/2009 05/20/2009 06/01/2009
1 1001 101.00 102.00 105.00 NULL
2 1001 41.00 44.00 NULL NULL
3 1001 NULL NULL NULL 330.00
Example 4: WHY 2 SELECT STATEMENTS IN PIVOT?
Consider Example 1.
Result Table
Id Area Result
1 A Pass
2 B Fail
3 A Pass
4 B Pass
5 A Fail
- SELECT * FROM dbo.Result
- PIVOT ( count(Id) FOR Result in (Pass, Fail) ) AS RESULT
- SELECT * FROM
- ( SELECT * FROM dbo.Result )
- AS P
- PIVOT ( count(Id) FOR Result in (Pass, Fail) ) AS RESULT
Area Pass Fail
A 2 1
B 1 1
The second statement has two select statements. Why do we need two select statements?
- SELECT * FROM dbo.Result
- PIVOT ( count(Id) FOR Result in (Pass, Fail) ) AS RESULT
- SELECT * FROM dbo.Result
- PIVOT ( count(Id) FOR Result in (Pass, Fail) ) AS RESULT
- SELECT * FROM dbo.Result
- where Area = 'A' -- ERROR
- PIVOT ( count(Id) FOR Result in (Pass, Fail) ) AS RESULT
- SELECT * FROM
- (SELECT * FROM dbo.Result WHERE Area = 'A') as P
- PIVOT ( count(Id) FOR Result in (Pass, Fail) ) AS RESULT
Area Pass Fail
A 2 1
Notes
The following produces an error:

ib belladPosted May 8, 2015, 8:19 AM
How to display one week(day by day) attendance report of student
ib belladPosted May 8, 2015, 8:18 AM
Nice..,How to display one week attendance of student