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: Today @ 5:43 PM
Points: 1,783, Visits: 5,670
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


  • MMGrid Addin
  • MMNose Addin


  • 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: Today @ 5:43 PM
    Points: 1,783, Visits: 5,670
    dyarwood (7/19/2013)
    Thank you!

    I thought I tried using an inline case statement earlier but yours works ;)


    You're welcome.


    MM


  • MMGrid Addin
  • MMNose Addin


  • 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