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 |
evanburen
Posting Yak Master
167 Posts |
Posted - 2009-03-11 : 14:17:33
|
HiI have a field in my database named divOrder which contains values like below and I want to count the number of times each one occurs. For example, I want the result to be something like Div1 20, Div2 8, Div3 12 etc. Possible values are Div1-Div15.DivOrder (varchar(250)Div5,Div6Div5,Div6Div5,Div6Div6,Div10,Div13,Div16Div3,Div5,Div6,Div9,Div10Thank you |
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-03-11 : 15:41:53
|
Can one row have more than 1 Div1 or Div2 etc? |
|
|
evanburen
Posting Yak Master
167 Posts |
Posted - 2009-03-11 : 15:56:53
|
No, there could be only one of each. Thanks. |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-03-11 : 16:03:11
|
Try this..select sum(case charindex('Div1',divorder) when 0 then 0 else 1 end) as Div1Count, sum(case charindex('Div2',divorder) when 0 then 0 else 1 end) as Div2Count, sum(case charindex('Div3',divorder) when 0 then 0 else 1 end) as Div3Count, sum(case charindex('Div4',divorder) when 0 then 0 else 1 end) as Div4Countand so on till 15 from urtable |
|
|
evanburen
Posting Yak Master
167 Posts |
Posted - 2009-03-11 : 16:09:43
|
That's exactly what I want. Thanks very much! |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-03-11 : 16:47:53
|
Welcome |
|
|
|
|
|
|
|