Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How can I combine results of COUNT operations into 1 output? Expand / Collapse
Author
Message
Posted Friday, July 19, 2013 3:22 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, July 19, 2013 5:49 PM
Points: 2, Visits: 3
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
Post #1475699
Posted Friday, July 19, 2013 5:41 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 5:32 PM
Points: 1,967, Visits: 6,619
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);


  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw
  • Post #1475716
    Posted Friday, July 19, 2013 5:56 PM
    Forum Newbie

    Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

    Group: General Forum Members
    Last Login: Friday, July 19, 2013 5:49 PM
    Points: 2, Visits: 3
    Thank you!

    I thought I tried using an inline case statement earlier but yours works ;)
    Post #1475717
    Posted Friday, July 19, 2013 6:01 PM


    SSCommitted

    SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

    Group: General Forum Members
    Last Login: Yesterday @ 5:32 PM
    Points: 1,967, Visits: 6,619
    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);


  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw
  • Post #1475718
    « Prev Topic | Next Topic »

    Add to briefcase

    Permissions Expand / Collapse