Home > DeveloperSection > Forums > How To Use Pivot In SQL Query.
Ankit Singh

Total Post:341

Points:2389
Posted on    October-29-2015 10:41 PM

 .NET SQL Server 
Ratings:


 1 Reply(s)
 282  View(s)
Rate this:
Can anyone please help me how to solve this problem.


aditya kumar Patel

Total Post:242

Points:1722
Posted on    October-29-2015 10:45 PM

lets Step one create order table

create table [Order]

(

 OrderID int primary key identity(1,1),

 Orderdate datetime,

 amount decimal(10,2)

)

now select sample query from order table

 

SELECT year(Orderdate) as [year], left(datename(month,Orderdate),3)as [month],

amount as Amount FROM [Order] as InvoiceResult

Now write pivot Query .

SELECT *

FROM (

    SELECT

        year(Orderdate) as [year],left(datename(month,Orderdate),3)as [month],

        amount as Amount

    FROM [Order]

) as s

PIVOT

(

    SUM(Amount)

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

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

)AS Result

 


Don't want to miss updates? Please click the below button!

Follow MindStick