Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 sql pivot query

Author  Topic 

mxfrail
Yak Posting Veteran

84 Posts

Posted - 2009-12-11 : 15:36:22
I am trying to turn this query into a pivot.

SELECT distinct left(program,5) + '0' as Program, month(proddate) as [Month]
FROM vw_FPO_Automation_2010
WHERE (CustNo = 'mdibos')

My goal is that I get this as a return

Program Jan Feb Mar Apr ....
100000 1 1 0 0
110000 0 1 0 0

Basically if its put 1s under the month otherwise put 0s in.

If anyone can help that would be great. Or if I am not explaining it well.


Right now I get -

100000 1
100000 2
100000 3
110000 2
120000 1
120000 3

etc.

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-12-11 : 15:44:25
One way...

SELECT left(program,5) + '0' as Program, 
max(case when month(proddate) = 1 then 1 else 0 end) as [Jan],
max(case when month(proddate) = 2 then 1 else 0 end) as [Feb],
.
.
.
max(case when month(proddate) = 12 then 1 else 0 end) as [Dec]
FROM vw_FPO_Automation_2010
WHERE (CustNo = 'mdibos')
group by (left(program,5) + '0')
Go to Top of Page

mxfrail
Yak Posting Veteran

84 Posts

Posted - 2009-12-11 : 15:55:27
perfect. thanks!
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-12-11 : 16:11:59
Np..You're welcome.
Go to Top of Page
   

- Advertisement -