Sunday 11 February 2018

How to use PIVOT in SQL


PIVOT in SQL:-

  • Pivot help us to transform or display data row  to column level. 
  • It help us to generate Multidimensional report.
  • We can generate Month & Year wise summary of large data.
Here we are going to display customer data month wise.

Query as below: -

Select * from CustomerData


Now we will use PIVOT in query:-

SELECT *

FROM (

SELECT name, mobileno, left(datename(month,dtDate),3)as [month], amount FROM CustomerData

) as s

PIVOT

(

SUM(amount)

FOR [month] IN (jan, feb, mar, apr,

may, jun, jul, aug, sep, oct, nov, dec)

)AS pvt
Query output as below:-






















No comments:

Post a Comment