Friday, 17 May 2013

Pivot in sql server 2008

Press MePress Me
Press Me
Press Me
Press Me
 This table name is piv


select *
from
(
select  month,saleamount from piv
) as a
pivot
(
sum(saleamount) for month  in([JAN],[FEB],[MAR],[APR],[MAY],[JUN],[JUL],[AUG])
)
as b
Another example
this table name is product

SELECT  * from
(
select 
 left(DATENAME(month, DATE),3) AS month,
price FROM    PRODUCT
) as a
pivot
(
sum(price) for month in([JAN],[FEB],[MAR])
)
as b
 Another example
SELECT  * from
(
select   datename(year,date) as year,
 left(DATENAME(month, DATE),3) AS month,
price FROM    PRODUCT
) as a
pivot
(
sum(price) for month in([JAN],[FEB],[MAR])
)
as b
Result

No comments:

Post a Comment