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


SUM Case statement where COUNT inside CASE statement


SUM Case statement where COUNT inside CASE statement

Author
Message
keyun
keyun
Mr or Mrs. 500
Mr or Mrs. 500 (591 reputation)Mr or Mrs. 500 (591 reputation)Mr or Mrs. 500 (591 reputation)Mr or Mrs. 500 (591 reputation)Mr or Mrs. 500 (591 reputation)Mr or Mrs. 500 (591 reputation)Mr or Mrs. 500 (591 reputation)Mr or Mrs. 500 (591 reputation)

Group: General Forum Members
Points: 591 Visits: 236
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.
GSquared
GSquared
SSC Guru
SSC Guru (58K reputation)SSC Guru (58K reputation)SSC Guru (58K reputation)SSC Guru (58K reputation)SSC Guru (58K reputation)SSC Guru (58K reputation)SSC Guru (58K reputation)SSC Guru (58K reputation)

Group: General Forum Members
Points: 58365 Visits: 9730
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
The Dixie Flatline
The Dixie Flatline
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13064 Visits: 6903
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
keyun
keyun
Mr or Mrs. 500
Mr or Mrs. 500 (591 reputation)Mr or Mrs. 500 (591 reputation)Mr or Mrs. 500 (591 reputation)Mr or Mrs. 500 (591 reputation)Mr or Mrs. 500 (591 reputation)Mr or Mrs. 500 (591 reputation)Mr or Mrs. 500 (591 reputation)Mr or Mrs. 500 (591 reputation)

Group: General Forum Members
Points: 591 Visits: 236
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..
The Dixie Flatline
The Dixie Flatline
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13064 Visits: 6903
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
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