Use SUM() with GROUP BY?

  • 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.

  • 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
  • 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.

  • 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

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]

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

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