Friday, November 27, 2009

PIVOT Queries

Pivot is basically used for the purpose of Presentation of data in the reports, and by using PIVOT the case and group by clauses are no longer useful

Following is an example of PIVOT table for getting the SLA for the different application

create a table
CREATE TABLE [dbo].[WeeklyStatus](
[Ticket] [int] NOT NULL,
[SLA] [int] NULL,
[Module] [nvarchar](50) )
make Ticket and Module pair as primary key

Insert few elements in the table and run the following query to get the no of tickets meet SLA for the application

select [Module],
[0] as SLAnotMeet,
[1] as SLAMeet
from (select [Module],[SLA],[Ticket] from WeeklyStatus) s
PIVOT
( count([Ticket]) for [SLA] in ([0],[1])) ws
order by [Module]

No comments:

Post a Comment