| Author |
Topic |
|
lauramayer
Posting Yak Master
152 Posts |
Posted - 2006-04-13 : 16:35:11
|
I'm really not sure how to ask this so here goes:I have a field CallReason that can have many values like so:Login,Instructions,Software it can have a litle as one. What I need to do is separate that out so it counts each value like this:select Software = SUM(CASE WHEN CallReason like '%Software%' THEN 1 ELSE 0 END), Hardware = SUM(CASE WHEN CallReason like '%Hardware%' THEN 1 ELSE 0 END), Email = SUM(CASE WHEN CallReason like '%Email%' THEN 1 ELSE 0 END), Instructions = SUM(CASE WHEN CallReason like '%Instructions%' THEN 1 ELSE 0 END),MissingReport = SUM(CASE WHEN CallReason like '%MissingReport%' THEN 1 ELSE 0 END), SoftwareUpgrade = SUM(CASE WHEN CallReason like '%SoftwareUpgrade%' THEN 1 ELSE 0 END),Login = SUM(CASE WHEN CallReason like '%Login%' THEN 1 ELSE 0 END),Other = SUM(CASE WHEN CallReason like '%Other%' THEN 1 ELSE 0 END)from dbo.tblISSatisfaction I get this result:Software Hardware Email ----------- ----------- ----------- 2 0 1 Which is good but now I need to take that and make it two columns like so:CallReason CountSoftware 2Hardware 0Email 1 If someone could point me in the right direction I would greatly appreciate it.Thanks so muchLaura |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-04-13 : 19:10:44
|
[code]select CallReason, count(*)from tblISSatisfactiongroup by CallReason[/code] KH |
 |
|
|
lauramayer
Posting Yak Master
152 Posts |
Posted - 2006-04-14 : 07:22:15
|
Thanks for that, but I need to have them separated. This just gets me the distinct groupings. Instead of CallReason CountLogin 2Instructions 2Software 2Email 1 I get:CallReason -------------------------------------------------- ----------- Email 1Login,Instructions,Software 2 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
lauramayer
Posting Yak Master
152 Posts |
Posted - 2006-04-14 : 07:56:53
|
| I guess I really am confused. There already concatenated in the field. I need to break them apart then count them. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
lauramayer
Posting Yak Master
152 Posts |
Posted - 2006-04-14 : 09:20:16
|
| Thanks to you both,but I dont understand either solution.Thanks anyway. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-04-14 : 09:58:38
|
Can you post your table structre, some sample data and the expected result KH |
 |
|
|
lauramayer
Posting Yak Master
152 Posts |
Posted - 2006-04-14 : 10:44:50
|
Thanks for that:CREATE TABLE [dbo].[tblISSatisfaction] ( [CustSatID] [int] IDENTITY (1, 1) NOT NULL , [CallReason] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,) ON [PRIMARY]GOINSERT INTO tblISSatisfaction (CallReason) VALUES ('Hardware,Software')INSERT INTO tblISSatisfaction (CallReason) VALUES ('Hardware,Software')INSERT INTO tblISSatisfaction (CallReason) VALUES ('email')I'd like it to end up like this:CallReason CountSoftware 2Hardware 2Email 1 And it might not even be possible which could explain my complete confusion.Thanks againLaura |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-04-14 : 11:50:59
|
[code]create function CSVStr(@str varchar(8000), @word_no int)returns varchar(100)asbegin declare @word varchar(100), @s int, @c int, @i int select @i = 1 select @s = 1 select @c = -1 while (@i <= @word_no and @c <> 0) begin select @s = @c + 1 select @c = charindex(',', @str + ',', @s) select @i = @i + 1 end select @word = substring(@str, @s, @c - @s) where @c <> 0 return @wordendgoselect dbo.CSVStr(CallReason, NUMBER), count(*)from tblISSatisfaction, dbo.F_TABLE_NUMBER_RANGE(1, 10)where dbo.CSVStr(CallReason, NUMBER) is not nullgroup by dbo.CSVStr(CallReason, NUMBER)[/code]the F_TABLE_NUMBER_RANGE can be obtain from here http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=47685 KH |
 |
|
|
lauramayer
Posting Yak Master
152 Posts |
Posted - 2006-04-14 : 12:08:49
|
| Wow, I cant thank you enough! Thast is just it. I thought it was a simple fix, the complexity behind making something I thought was simple is astounding. Thank you for taking the time to help.Laura |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|