Solved

MS SQL - Using SUM and CASE to get counts

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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, …
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

948 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

18 Experts available now in Live!

Get 1:1 Help Now