SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Summing rows to combine them


Summing rows to combine them

Author
Message
matak
matak
SSC Eights!
SSC Eights! (816 reputation)SSC Eights! (816 reputation)SSC Eights! (816 reputation)SSC Eights! (816 reputation)SSC Eights! (816 reputation)SSC Eights! (816 reputation)SSC Eights! (816 reputation)SSC Eights! (816 reputation)

Group: General Forum Members
Points: 816 Visits: 4287
Hi,

Im trying to find a better way to do this but cant quite get there.
Here is some quick data setup.


if OBJECT_ID('tempdb.dbo.#temp') is not null
drop table #temp;

create table #temp (
[state] char(2),
[county] char(3),
[sex] varchar(6),
[class] int,
[agegroup] varchar(15),
[total] int
)

insert into #temp
select '01', '001', 'male', 1, '05-09 yrs', 3 union all
select '01', '001', 'female', 1, '05-09 yrs', 4 union all
select '01', '001', 'male', 1, '15-17 yrs', 5 union all
select '01', '001', 'female', 1, '15-17 yrs', 6 union all
select '01', '001', 'male', 1, '18-20 yrs', 7 union all
select '01', '001', 'female', 1, '21-24 yrs', 3 union all
select '01', '001', 'male', 1, '21-24 yrs', 3 union all
select '01', '001', 'male', 2, '05-09 yrs', 4 union all
select '01', '001', 'female', 2, '05-09 yrs', 5 union all
select '01', '001', 'male', 2, '15-17 yrs', 6 union all
select '01', '001', 'female', 2, '15-17 yrs', 7 union all
select '01', '001', 'male', 2, '18-20 yrs', 8 union all
select '01', '001', 'female', 2, '21-24 yrs', 4 union all
select '01', '001', 'male', 2, '21-24 yrs', 4



Here is what i currently have. It also gives the result im after
I was thinking of sum and case but not quite sure where to start or even if its the right way to go.


select [State], County, Sex, class, '15-24 yrs' AS [Agegroup], sum(total) AS [total]
from #temp
where Agegroup in ('15-17 yrs', '18-20 yrs', '21-24 yrs')
group by [State], County, Sex, class

union all

select [State], County, Sex, class, Agegroup, sum(total) AS [total]
from #temp
where Agegroup not in ('15-17 yrs', '18-20 yrs', '21-24 yrs')
group by [State], County, Sex, class, Agegroup


Mark Cowne
Mark Cowne
SSCertifiable
SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)

Group: General Forum Members
Points: 6573 Visits: 25602
Another way

with cte as (
select [State], County, Sex, class,
case when Agegroup in ('15-17 yrs', '18-20 yrs', '21-24 yrs') then '15-24 yrs' else Agegroup end AS [Agegroup],
[total]
from #temp)
select [State], County, Sex, class, Agegroup, sum(total) AS [total]
from cte
group by [State], County, Sex, class, Agegroup



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




matak
matak
SSC Eights!
SSC Eights! (816 reputation)SSC Eights! (816 reputation)SSC Eights! (816 reputation)SSC Eights! (816 reputation)SSC Eights! (816 reputation)SSC Eights! (816 reputation)SSC Eights! (816 reputation)SSC Eights! (816 reputation)

Group: General Forum Members
Points: 816 Visits: 4287
Thanks - this is a lot nicer than what i had.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search