Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Use SUM() with GROUP BY?


Use SUM() with GROUP BY?

Author
Message
ucltech
ucltech
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 18
Is it possible to use SUM() in a SELECT query that uses a series of CASE statements to group data together? I have a procedure that generates an age-range report using case into different groups, I would like to combine that report with a report that SUM's 7 different fields by category. Right now, I take the output of the age -range report and through code, sum up all of the fields by separate calls. It's decently fast but I would like to know if I can move the SUM calculation into the case query if that's even possible.
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8546 Visits: 18134
If I understand correctly, you need something like this.

SELECT SUM( CASE WHEN age BETWEEN 18 AND 25 THEN MyValue END)  AS [18-25],
SUM( CASE WHEN age BETWEEN 26 AND 35 THEN MyValue END) AS [26-35],
SUM( CASE WHEN age BETWEEN 36 AND 45 THEN MyValue END) AS [36-45],
SUM( CASE WHEN age > 45 THEN MyValue END) AS [18-25]
FROM myTable



Otherwise, you should post more details. Please read the article linked on my signature to find out how to get better help. ;-)


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
ucltech
ucltech
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 18
Yeah a code example would have been a good start had I remembered to cut and paste it in. The case procedure looks like this:

WITH cte AS (SELECT DATEDIFF(YEAR, c.DOB1,GETDATE()) AS AGE FROM ClientsDetails c)
SELECT CASE WHEN cte.Age < 30 THEN '18-29'
WHEN cte.Age BETWEEN 30 AND 39 THEN '30-39'
[...]
ELSE '80+' END AS [Age],
COUNT(*) AS [Count]
FROM cte
GROUP BY CASE WHEN cte.Age < 30 THEN '18-29'
WHEN cte.Age BETWEEN 30 AND 39 THEN '30-39'
[...]
ELSE '80+'

So I get back "18-29, 6 - 30-39 55" ect.

Another report groups [ClientsDetails].Classification with table [Supply] and a SUM of 7 different fields in that table grouoped by classification. What I would like is to combine the CASE report to generate an age report that shows "18-29, 15455 - 30-39 22899" which is the AGE case, SUM with fields (1-7) from Supply. I've added in a SUM with a join but the group by tells me that it's not part of the aggregate function. It works in code but I would like to move it all back to SQL if possible.
Erland Sommarskog
Erland Sommarskog
SSC Eights!
SSC Eights! (935 reputation)SSC Eights! (935 reputation)SSC Eights! (935 reputation)SSC Eights! (935 reputation)SSC Eights! (935 reputation)SSC Eights! (935 reputation)SSC Eights! (935 reputation)SSC Eights! (935 reputation)

Group: General Forum Members
Points: 935 Visits: 866
It's difficult to understand what you have problem with when you only describe the query rather than showing the code.

But the query you posted can be simplified by adding another CTE:

WITH cte AS (SELECT DATEDIFF(YEAR, c.DOB1,GETDATE()) AS AGE FROM ClientsDetails c),
cte2 AS (
SELECT CASE WHEN cte.Age < 30 THEN '18-29'
WHEN cte.Age BETWEEN 30 AND 39 THEN '30-39'
[...]
ELSE '80+' END AS [Age]
FROM cte
)
SELECT Age, COUNT(*)
FROM cte2
GROUP BY Age

Erland Sommarskog, SQL Server MVP, www.sommarskog.se
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