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

Use SUM() with GROUP BY? Expand / Collapse
Author
Message
Posted Monday, September 02, 2013 7:47 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, September 02, 2013 4:20 PM
Points: 2, Visits: 10
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.
Post #1490627
Posted Monday, September 02, 2013 8:40 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 10:00 PM
Points: 2,763, Visits: 5,905
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.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1490638
Posted Monday, September 02, 2013 8:49 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, September 02, 2013 4:20 PM
Points: 2, Visits: 10
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.
Post #1490639
Posted Monday, September 02, 2013 4:19 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: Yesterday @ 4:43 AM
Points: 756, Visits: 631
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
Post #1490706
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse