Tom Sage
asked on
MS SQL - Using SUM and CASE to get counts
Hello,
I am using SUM (CASE... to get counts. The query does not seem to return the correct values. When I do just Select COUNT(*) from Table Where (Single criteria), it returns a different value.
What am I doing wrong here?
Thanks
I am using SUM (CASE... to get counts. The query does not seem to return the correct values. When I do just Select COUNT(*) from Table Where (Single criteria), it returns a different value.
What am I doing wrong here?
Thanks
SELECT
SUM(CASE WHEN (DMStype = 'ADP' And PollStatus = 'Complete') then 1 else 0 end) AS ADPcomp,
SUM(CASE WHEN (DMStype = 'ADP') then 1 else 0 end) AS ADPtot,
SUM(CASE WHEN (DMStype IN ('RR','RRC') And PollStatus = 'Complete') then 1 else 0 end) AS RRComp,
SUM(CASE WHEN (DMStype IN ('RR','RRC')) then 1 else 0 end) AS RRtot
From MyTable
Where DMStype IN ('ADP','RR','RRC') And DealerStatus IN ('Prod','IPRO','Stage')
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you