SUM Case statement where COUNT inside CASE statement

  • hi,

    Need some expertice or advice,

    I am trying to SUM my case statement where I have COUNT function inside the CASE statement.

    Here is my sample code:

    SELECT d.name,

    CASE WHEN a.procedure_code LIKE '%F' THEN COUNT(DISTINCT a.ID) ELSE 0 END AS NumberOf_Code,

    SUM(DISTINCT c.number_claims) AS number_claims,

    COUNT(DISTINCT c.filename) AS number_files

    FROM TableOne AS a

    INNER JOIN TableTwo AS b with (Nolock)

    ON a.claim_id = b.id

    INNER JOIN TableThree AS c with (Nolock)

    ON b.transaction_id = c.id

    INNER JOIN TableFour AS d with (Nolock)

    ON c.provider_id = d.id

    GROUP BY d.name,a.procedure_code

    Now, if i run the query I am getting duplicate Row and I know its due to not summing up the other columns. So, I tried to put my CASE statement in SUM ( Case......) AS NumberOf_Code but the sql giving me an error "Msg 130, Level 15, State 1, Line 1

    Cannot perform an aggregate function on an expression containing an aggregate or a subquery."

    There is several way to achive the goal and the one I know is to put this data into temp table and use temp table for further group by but that will aditional step< I am thinking to do achive this result in one single query...

    Any thought would be greatly appriciated.

  • Go with the temp table idea. It's the best option.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Or, how about this? The cte does the grouping like a temp table, but without any logging overhead.

    ;with cte as (

    SELECT d.name,

    COUNT(DISTINCT a.ID) AS DistinctIDs,

    SUM(DISTINCT c.number_claims) AS number_claims,

    COUNT(DISTINCT c.filename) AS number_files

    FROM TableOne AS a

    INNER JOIN TableTwo AS b with (Nolock)

    ON a.claim_id = b.id

    INNER JOIN TableThree AS c with (Nolock)

    ON b.transaction_id = c.id

    INNER JOIN TableFour AS d with (Nolock)

    ON c.provider_id = d.id

    GROUP BY d.name,a.procedure_code

    )

    select d.name,

    CASE WHEN a.procedure_code LIKE '%F' THEN DistinctIDs ELSE 0 END AS NumberOf_Code,

    number_claims,number_files

    from cte

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

  • Bob -Thanks a million...it work...Awsome...I appriciated....The only thing i have to do is i have to tiwic a query littlebit and one single change and it worked perfectly...thanks a lot bcz I didnt want to create a temp table so..it work perfect for my Need...

    Thanks a Billionn..

  • No problem. It's not *that* big a deal, but you're welcome.

    CTEs are our friends. 😉

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

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

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