Solved

MS SQL - Using SUM and CASE to get counts

Posted on 2011-03-01
4
409 Views
Last Modified: 2012-05-11
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
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')

Open in new window

0
Comment
Question by:MisterT25
4 Comments
 
LVL 22

Accepted Solution

by:
Thomasian earned 167 total points
ID: 35014026
The query looks ok with me.

Can you post the count query you compared the results with?
0
 
LVL 24

Assisted Solution

by:jimyX
jimyX earned 167 total points
ID: 35014154
Lines 3 and 5 are returning wrong sum. Isn't it?
Most likely you forgot about the condition:
Where DMStype IN ('ADP','RR','RRC') And DealerStatus IN ('Prod','IPRO','Stage')
So you are expecting the sum of all the rows of 'ADP', 'RR', and 'RRC' but the condition DealerStatus IN ('Prod','IPRO','Stage') will cut the results down which confuses you.
0
 

Assisted Solution

by:pavan_kumar_kokkiragedda
pavan_kumar_kokkiragedda earned 166 total points
ID: 35014755
The querylooks good to me.And the most likely issue is while running the select count(*),you might have missed the dealer status criteria.

hope this helps.
output
Regards,
Pavan
0
 

Author Closing Comment

by:MisterT25
ID: 35016568
Thanks to everyone.   The counts are working ok.  My mistake.  Sorry.

Thank you
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question