Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
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_2010WHERE (CustNo = 'mdibos')My goal is that I get this as a returnProgram Jan Feb Mar Apr ....100000 1 1 0 0 110000 0 1 0 0Basically 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 1100000 2100000 3110000 2120000 1120000 3etc.
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_2010WHERE (CustNo = 'mdibos')group by (left(program,5) + '0')