How do you do a GROUP BY on a value that's changed by a CASE?

  • I've got a SELECT statement which I had hoped would give me what I needed, for counting the number of types of races of people who come into our agency. Here's what the SELECT currently looks like, with values specific only for the first quarter of 2014:

    select atr.race,

    case atr.Race

    when 32 then 'White'

    when 1 then 'Black/African American'

    when 2 then 'Asian'

    when 4 then 'American Indian/Alaskan Native'

    when 16 then 'American Indian/Alaskan Native'

    when 8 then 'Native Hawaiian/Other Pacific Islander'

    when 36 then 'American Indian/Alaskan Native and White'

    when 48 then 'American Indian/Alaskan Native and White'

    when 34 then 'Asian and White'

    when 33 then 'Black/African American and White'

    when 5 then 'American Indian/Alaskan Native and Black/African American'

    when 17 then 'American Indian/Alaskan Native and Black/African American'

    when 10 then 'Asian/Pacific Islander'

    else 'Other Multi-Racial'

    end as RaceDescription,

    yesno.YesNoDesc as 'Hispanic Yes/No', count(*) as racetotal

    from dbo.asigeneral as asi

    inner join dbo.asiatrsupplement as atr

    on atr.clientnumber = asi.clientnumber

    and atr.casenumber = asi.casenumber

    left join dbo.ASICodesATRYesNo yesno

    on atr.hispanicyesno = yesno.YesNo

    here asicompleted between '2014-1-1' and '2014-3-31'

    group by atr.race, yesno.YesNoDesc

    order by atr.race, yesno.YesNoDesc desc

    This is a part of a stored procedure (properly parameterized, of course) which is used in a SSRS report. I don't show the individual values returned by atr.Race; the user isn't interested in seeing them. I had hoped that this would capture all of the racial types that come through our doors.

    Well, I'm wrong. Now I'd like to do a GROUP BY RaceDescription, but that isn't valid. So how do I get it done?

    Kindest Regards, Rod Connect with me on LinkedIn.

  • You have 2 options:

    First one:

    WITH CTE AS(

    select case atr.Race

    when 32 then 'White'

    when 1 then 'Black/African American'

    when 2 then 'Asian'

    when 4 then 'American Indian/Alaskan Native'

    when 16 then 'American Indian/Alaskan Native'

    when 8 then 'Native Hawaiian/Other Pacific Islander'

    when 36 then 'American Indian/Alaskan Native and White'

    when 48 then 'American Indian/Alaskan Native and White'

    when 34 then 'Asian and White'

    when 33 then 'Black/African American and White'

    when 5 then 'American Indian/Alaskan Native and Black/African American'

    when 17 then 'American Indian/Alaskan Native and Black/African American'

    when 10 then 'Asian/Pacific Islander'

    else 'Other Multi-Racial'

    end as RaceDescription,

    yesno.YesNoDesc as 'Hispanic Yes/No'

    from dbo.asigeneral as asi

    inner join dbo.asiatrsupplement as atr

    on atr.clientnumber = asi.clientnumber

    and atr.casenumber = asi.casenumber

    left join dbo.ASICodesATRYesNo yesno

    on atr.hispanicyesno = yesno.YesNo

    where asicompleted between '2014-1-1' and '2014-3-31'

    )

    SELECT RaceDescription,

    YesNoDesc,

    count(*) as racetotal

    FROM CTE

    group by race, YesNoDesc

    order by race, YesNoDesc desc

    2nd option:

    select case atr.Race

    when 32 then 'White'

    when 1 then 'Black/African American'

    when 2 then 'Asian'

    when 4 then 'American Indian/Alaskan Native'

    when 16 then 'American Indian/Alaskan Native'

    when 8 then 'Native Hawaiian/Other Pacific Islander'

    when 36 then 'American Indian/Alaskan Native and White'

    when 48 then 'American Indian/Alaskan Native and White'

    when 34 then 'Asian and White'

    when 33 then 'Black/African American and White'

    when 5 then 'American Indian/Alaskan Native and Black/African American'

    when 17 then 'American Indian/Alaskan Native and Black/African American'

    when 10 then 'Asian/Pacific Islander'

    else 'Other Multi-Racial'

    end as RaceDescription,

    yesno.YesNoDesc as 'Hispanic Yes/No',

    count(*) as racetotal

    from dbo.asigeneral as asi

    inner join dbo.asiatrsupplement as atr

    on atr.clientnumber = asi.clientnumber

    and atr.casenumber = asi.casenumber

    left join dbo.ASICodesATRYesNo yesno

    on atr.hispanicyesno = yesno.YesNo

    where asicompleted between '2014-1-1' and '2014-3-31'

    group by case atr.Race

    when 32 then 'White'

    when 1 then 'Black/African American'

    when 2 then 'Asian'

    when 4 then 'American Indian/Alaskan Native'

    when 16 then 'American Indian/Alaskan Native'

    when 8 then 'Native Hawaiian/Other Pacific Islander'

    when 36 then 'American Indian/Alaskan Native and White'

    when 48 then 'American Indian/Alaskan Native and White'

    when 34 then 'Asian and White'

    when 33 then 'Black/African American and White'

    when 5 then 'American Indian/Alaskan Native and Black/African American'

    when 17 then 'American Indian/Alaskan Native and Black/African American'

    when 10 then 'Asian/Pacific Islander'

    else 'Other Multi-Racial'

    end, YesNoDesc

    order by race, YesNoDesc desc

    Note that atr.Race was removed from the column list to be able to group by the descriptions.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Hi Luis,

    I like your two solutions. Especially the first one, which I take to be an example of using a common table expression. I've heard of CTE's, but have never used one. I tried that first, but unfortunately it gave me the following errors:

    Msg 207, Level 16, State 1, Line 31

    Invalid column name 'race'.

    Msg 207, Level 16, State 1, Line 31

    Invalid column name 'YesNoDesc'.

    Msg 207, Level 16, State 1, Line 28

    Invalid column name 'YesNoDesc'.

    Msg 207, Level 16, State 1, Line 32

    Invalid column name 'race'.

    The second one was closer to working, but it also gave me an error:

    Msg 8127, Level 16, State 1, Line 42

    Column "dbo.asiatrsupplement.Race" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.

    Kindest Regards, Rod Connect with me on LinkedIn.

  • The error has to do with the fact that it is now a CTE and is returning YesNoDesc field as something else. Try thisWITH CTE AS(

    select case atr.Race

    when 32 then 'White'

    when 1 then 'Black/African American'

    when 2 then 'Asian'

    when 4 then 'American Indian/Alaskan Native'

    when 16 then 'American Indian/Alaskan Native'

    when 8 then 'Native Hawaiian/Other Pacific Islander'

    when 36 then 'American Indian/Alaskan Native and White'

    when 48 then 'American Indian/Alaskan Native and White'

    when 34 then 'Asian and White'

    when 33 then 'Black/African American and White'

    when 5 then 'American Indian/Alaskan Native and Black/African American'

    when 17 then 'American Indian/Alaskan Native and Black/African American'

    when 10 then 'Asian/Pacific Islander'

    else 'Other Multi-Racial'

    end as RaceDescription,

    yesno.YesNoDesc as 'YesNoDesc'

    from dbo.asigeneral as asi

    inner join dbo.asiatrsupplement as atr

    on atr.clientnumber = asi.clientnumber

    and atr.casenumber = asi.casenumber

    left join dbo.ASICodesATRYesNo yesno

    on atr.hispanicyesno = yesno.YesNo

    where asicompleted between '2014-1-1' and '2014-3-31'

    )

    SELECT RaceDescription,

    YesNoDesc,

    count(*) as racetotal

    FROM CTE

    group by RaceDescription, YesNoDesc

    order by RaceDescription, YesNoDesc descNow this is me guess since there is no sample DDL and DML to test this query. In order to get a solid response back it is always better to provide sample scripts. Please read the first article in my signature



    Microsoft Certified Master - SQL Server 2008
    Follow me on twitter: @keith_tate

    Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • Ah, that makes sense, Keith, thank you.

    And sorry that I didn't post the table definitions as well as some data, to get a feel for what the query would likely return.

    Kindest Regards, Rod Connect with me on LinkedIn.

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

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