Solved

MS SQL - Using SUM and CASE to get counts

Posted on 2011-03-01
4
404 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now