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 do you do a GROUP BY on a value that's changed by a CASE? Expand / Collapse
Author
Message
Posted Thursday, April 3, 2014 3:30 PM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Sunday, November 9, 2014 7:49 AM
Points: 738, Visits: 1,306
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.
Post #1558277
Posted Thursday, April 3, 2014 3:40 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 4:45 PM
Points: 3,939, Visits: 8,934
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1558279
Posted Friday, April 4, 2014 8:12 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Sunday, November 9, 2014 7:49 AM
Points: 738, Visits: 1,306
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.
Post #1558524
Posted Friday, April 4, 2014 8:51 AM This worked for the OP Answer marked as solution


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Yesterday @ 11:59 AM
Points: 594, Visits: 933
The error has to do with the fact that it is now a CTE and is returning YesNoDesc field as something else. Try this
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 '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 desc

Now 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
Post #1558553
Posted Friday, April 4, 2014 9:11 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Sunday, November 9, 2014 7:49 AM
Points: 738, Visits: 1,306
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.
Post #1558565
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse