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.
| Author |
Topic |
|
dr223
Constraint Violating Yak Guru
444 Posts |
Posted - 2012-04-10 : 09:25:05
|
| Hi, I have a table as; IDno Code1 Z2 Z3 X4 X5 X6 Y7 YI want to write a count of each code value and have the results asX Y Z3 2 2Thank you |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2012-04-10 : 09:51:20
|
| You can also do with CASE statementsDECLARE @Table TABLE(IdNo tinyint,Code char(1))INSERT INTO @TableVALUES(1,'Z'),(2,'Z'),(3,'X'),(4,'X'),(5,'X'),(6,'Y'),(7,'Y') SELECT [X],[Y],[Z]FROM (select IdNo,code from @table ) p PIVOT ( Count(IDno) FOR Code in ([X],[Y],[Z]) ) as pvtJimEveryday I learn something that somebody else already knew |
 |
|
|
dr223
Constraint Violating Yak Guru
444 Posts |
Posted - 2012-04-10 : 09:54:03
|
| thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-04-10 : 15:24:27
|
| hope your columns will be fixed!------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|