HOW TO USE PIVOT IN SQL QUERY.

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
 587  View(s)
Ratings:
Rate this:
Can anyone please help me how to solve this problem.
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: How To Use Pivot In SQL Query.

    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

     

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!