How to Group By With multiple conditions

  • Hi,

    I need help with using COUNT with multiple criteria's.

    SELECT DISTINCT CaseStatus, CASE_GROUP_TOTAL = COUNT(CaseStatus)

    FROM MCJ

    WHERE Case_Group IN ('INPATIENT', 'Outpatient')

    GROUP BY CaseStatus, Case_Group

    This works however I am unable to tell which one is Inpatient and which one is outpatient without having to go to the data.

    Is there anything else I can add to show the breakdown for each Inpatient / Outpatient case status.

    Thanks for any help.

  • The information you give is quite limited so this is going to require some guesswork. But perhaps this works:

    SELECT CaseStatus, Case_Group, COUNT(CaseStatus) AS CASE_GROUP_TOTAL

    FROM dbo.MCJ

    WHERE Case_Group IN ('INPATIENT', 'Outpatient')

    GROUP BY CaseStatus, Case_Group;

    (untested)


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • If I have understood you correctly, is it as simple as this?....

    select CaseStatus, Case_Group, CASE_GROUP_TOTAL = COUNT(*)

    from MCJ

    WHERE Case_Group IN ('INPATIENT', 'Outpatient')

    GROUP BY CaseStatus, Case_Group

  • That works perfectly thank you so much.

    Also If i wanted to add in more criteria do i just simply add them into the Where statement.

    thanks again

  • That is correct - just add any additional criteria to the WHERE statement

  • thank you 🙂

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply