July 19, 2013 at 3:22 pm
Hi all, and thanks in advance for the help.
I'm trying to get a total count and a subset count from a column in a particular table.
The table is called History. I'm trying to get a count for the # of times a report has been run, and the number of times the report failed.
So, what I'm looking for would look like:
control_id total_count error_count
1 10 2
etc
The statements that I'm trying to merge are:
--retrieves total count
SELECT control_id, COUNT(*) AS total_count
FROM history
group by control_id order by control_id
-- retrieves error count
select COUNT(*) as error_count
from history
where action_details like '%Fail%'
group by control_id order by control_id
What I've been trying is a select statement with all 3 columns in it, but that doesn't work.
SELECT control_id, COUNT(*) AS total_count, (select COUNT(*), control_id as error_count from history where action_details like '%Fail%' group by control_id ) as Errors
from history
group by control_id order by control_id
I've tried putting the control_id inside the count function but that fails also.
SELECT control_id, COUNT(*) AS total_count, (select COUNT(control_id) as error_count from history where action_details like '%Fail%' group by control_id ) as Errors
from history
group by control_id order by control_id
July 19, 2013 at 5:41 pm
SELECT control_id, COUNT(*) AS total_count, SUM(CASE WHEN action_details like '%Fail%' THEN 1 ELSE 0 END) as error_count
FROM history
group by control_id order by control_id
MM
select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);
July 19, 2013 at 5:56 pm
Thank you!
I thought I tried using an inline case statement earlier but yours works 😉
July 19, 2013 at 6:01 pm
dyarwood (7/19/2013)
Thank you!I thought I tried using an inline case statement earlier but yours works 😉
You're welcome.
MM
select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply