Avatar of Samoin
Samoin
 asked on

Pivot

Experts,

I've a table with the following rows

Month              Yr                     a             b               c
JAN               2016                    1             2               3
DEC              2015                    4             5               6
NOV             2015                    7             8               9
......

And I need to pivot it to the following

Month      NOV      DEC       JAN
a              7             4              1
b              8             5              2
c              9             6              3


The pivot query I'm using is below

Select *
From (Select Month , a, b, c
From dbo.table) s
pivot
(
      sum(a)
      for Month in ([JAN],[FEB],[MAR],[APR],[MAY],[JUN],[JUL],[AUG],[SEP],[OCT],[NOV],[DEC])
) P;

And I'm getting the following results which is not what I want.

b      c      JAN      FEB      MAR      APR      MAY      JUN      JUL      AUG      SEP        OCT       NOV      DEC
0      0      0      0      0              0      0              0      0      0              0         0              0              0

Can anybody suggest what I'm doing incorrectly? Or assist with the right answer?

Thanks.
Microsoft SQL Server

Avatar of undefined
Last Comment
Samoin

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Brian Crowe

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Samoin

ASKER
Brian,

The column names are not in right order? Does MAX(Value) has anything to do with it.
Brian Crowe

The column order is determined by the first SELECT in the PIVOT query.  Just change them to whatever you need.
Samoin

ASKER
Thanks, worked for me.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck