May 1, 2010 at 4:36 pm
hi guys, i've got a table like this:
id - item - col1 - col2 - col3
1 - david -checked - unchecked - checked
2 - david -checked - checked - unchecked
3 - anne -unchecked -checked - checked
4 - peter -checked - checked - unchecked
5 - peter -unchecked - unchecked - unchecked
.
.
.
and i want to have the recordset like this(counting those with ticks on checkbox):
item - col1 - col2 - col3
david - 2 - 1 - 1
anne - 0 - 1 - 1
peter - 1 - 1 - 0
any tips guys? thanks in advance.
May 1, 2010 at 11:50 pm
DECLARE @data
TABLE (
id INTEGER PRIMARY KEY,
item VARCHAR(10) NOT NULL,
col1 VARCHAR(9) NOT NULL,
col2 VARCHAR(9) NOT NULL,
col3 VARCHAR(9) NOT NULL
);
INSERT @data
VALUES (1, 'david', 'checked', 'unchecked', 'checked'),
(2, 'david', 'checked', 'checked', 'unchecked'),
(3, 'anne', 'unchecked', 'checked', 'checked'),
(4, 'peter', 'checked', 'checked', 'unchecked'),
(5, 'peter', 'unchecked', 'unchecked', 'unchecked');
SELECT item,
COUNT(NULLIF(col1, 'unchecked')),
COUNT(NULLIF(col2, 'unchecked')),
COUNT(NULLIF(col3, 'unchecked'))
FROM @data
GROUP BY item
ORDER BY MIN(id);
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
May 3, 2010 at 4:09 am
May 3, 2010 at 4:14 am
dmoldovan (5/3/2010)
Seems to be solving a different problem from that asked in this thread though...?
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
May 3, 2010 at 4:57 am
Paul White NZ (5/3/2010)
dmoldovan (5/3/2010)
Also checkSeems to be solving a different problem from that asked in this thread though...?
It's a general overview of aggregating over several columns. For me it was a good resource.
May 3, 2010 at 5:39 am
dmoldovan (5/3/2010)
It's a general overview of aggregating over several columns. For me it was a good resource.
Yes, I agree. I just don't think it directly solves the particular problem raised here.
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
May 3, 2010 at 7:41 am
I still think this is a useful addition 🙂
May 3, 2010 at 9:43 am
dmoldovan (5/3/2010)
I still think this is a useful addition 🙂
Um, I agree (didn't I say that?) 😉
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply