Author |
Topic |
emailuser
Yak Posting Veteran
74 Posts |
Posted - 2013-02-07 : 12:29:08
|
Hi everyone , i think this is a fairly easy sql script but i cannot work it out , i have data in a table in the formatDept Type IT aFinance aIT aSales bSales cFinance cIT band need an output :Dept Type a Type b Type cIT 2 1 0Sales 0 1 1Finance 1 0 1 Any help greatly appreciated |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-02-07 : 16:19:24
|
[code]SELECT Dept, a AS [Type a], b AS [Type b], c AS [Type c]FROM TheTablePIVOT (COUNT([type]) FOR [Type] IN ([a],[b],[c]))P;[/code] |
|
|
emailuser
Yak Posting Veteran
74 Posts |
Posted - 2013-02-08 : 05:29:56
|
Hi James, many thanks for your reply , will this work for SQL2000 ? .. tried it but got errors |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-02-08 : 05:37:18
|
quote: Originally posted by emailuser Hi James, many thanks for your reply , will this work for SQL2000 ? .. tried it but got errors
Nope PIVOT is available only from 2005 onwardsfor 2000 you need to useSELECT Dept,SUM(CASE WHEN Type='a' THEN 1 ELSE 0 END) AS [Type a],SUM(CASE WHEN Type='b' THEN 1 ELSE 0 END) AS [Type b],SUM(CASE WHEN Type='c' THEN 1 ELSE 0 END) AS [Type c]FROM TableGROUP BY Dept ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
emailuser
Yak Posting Veteran
74 Posts |
Posted - 2013-02-13 : 11:32:26
|
thanks everyone and to visakh16 for the solution .. just upgraded to sql2005 over weekend so a few more options now :) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-02-13 : 11:33:59
|
ok...cool------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|