Friday, July 19, 2013

[SQL Server] How can I combine results of COUNT operations into 1 output?

[SQL Server] How can I combine results of COUNT operations into 1 output?


How can I combine results of COUNT operations into 1 output?

Posted: 19 Jul 2013 08:22 AM PDT

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_count1 10 2etcThe statements that I'm trying to merge are:--retrieves total countSELECT control_id, COUNT(*) AS total_countFROM historygroup by control_id order by control_id-- retrieves error countselect COUNT(*) as error_count from history where action_details like '%Fail%' group by control_id order by control_idWhat 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 Errorsfrom historygroup by control_id order by control_idI'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 Errorsfrom historygroup by control_id order by control_id

No comments:

Post a Comment

Search This Blog