pivot in sql server 2008

PIVOT in SQL Server

PIVOT() is used to convert  the rows to columns in SQL Server. 


The example as give below. 

SELECT ISNULL(PVT.JANUARY,'0') AS JANUARY,ISNULL(PVT.FEBRUARY,'0')FEBRUARY,ISNULL(PVT.MARCH,'0')MARCH,
ISNULL(PVT.APRIL,'0')APRIL,ISNULL(PVT.MAY,'0')MAY,ISNULL(PVT.JUNE,'0')JUNE,ISNULL(PVT.JULY,'0')JULY,
ISNULL(PVT.AUGUST,'0')AUGUST,ISNULL(PVT.SEPTEMBER,'0')SEPTEMBER,ISNULL(PVT.OCTOBER,'0') OCTOBER,
ISNULL(PVT.NOVEMBER,'0')NOVEMBER,ISNULL(PVT.DECEMBER,'0') DECEMBER
FROM(SELECT DATENAME(MONTH,CREATEDDATE) AS [MONTH],COUNT(*) AS CCOUNT FROM COMPANY
WHERE TENANTID=1 AND ISNULL(ISDELETED, 0) <> 1
GROUP BY   DATENAME(MONTH,CREATEDDATE)
) AS TMP
PIVOT(SUM(CCOUNT) FOR [MONTH]
IN(JANUARY,FEBRUARY,MARCH,APRIL,MAY,JUNE,JULY,AUGUST,SEPTEMBER,OCTOBER,NOVEMBER,DECEMBER)) AS PVT  
ANIL SINGH

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Buy Books Online at Best Prices

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.