Solved

Update to SQL - Count Values of Zero

Posted on 2006-07-10
4
367 Views
Last Modified: 2012-05-05
I have a query that currently counts distinct Key3:

SELECT username, type, count(distinct r.key2) as AdCount, count(distinct r.key3) as SubCount,   CASE WHEN DATEPART(hour, ra.action_time) >= 12 THEN  CONVERT(DATETIME, CONVERT(VARCHAR, ra.action_time, 102), 102)
                ELSE   CONVERT(DATETIME, CONVERT(VARCHAR, DATEADD(Day, -1,ra.action_time), 102), 102)  
             END   AS ShiftDate, SUBSTRING(CONVERT(char(12),ra.action_time,108),1,2) as Hour, count(*) as RecordCount

This works when the key is used.  But there are records where the Key3 is 0 (zero).  There are cases where there are multiple Key3s and I want it to add these up.  Can you show me how to change the code above to accumulate the Key3 values when they = 0?  

Scotto13
0
Comment
Question by:Scotto13
4 Comments
 
LVL 12

Expert Comment

by:Einstine98
ID: 17076475
count will count them all (0) and Multiples...

so

Key3
0
0
0
4
5
6
6
6

Count(key3) = 8
0
 
LVL 25

Accepted Solution

by:
jrb1 earned 500 total points
ID: 17076483
SELECT username, type, count(distinct r.key2) as AdCount, count(distinct r.key3) as SubCount,  
sum(case when r.key3=0 then 1 else 0 end) as Key3ZeroCount,  
CASE WHEN DATEPART(hour, ra.action_time) >= 12 THEN  CONVERT(DATETIME, CONVERT(VARCHAR, ra.action_time, 102), 102)
                ELSE   CONVERT(DATETIME, CONVERT(VARCHAR, DATEADD(Day, -1,ra.action_time), 102), 102)  
             END   AS ShiftDate, SUBSTRING(CONVERT(char(12),ra.action_time,108),1,2) as Hour, count(*) as RecordCount

You can use a CASE and SUM to add these up.
0
 
LVL 26

Expert Comment

by:DireOrbAnt
ID: 17076509
Unsure here, simply remove distinct?

SELECT username, type, count(distinct r.key2) as AdCount, count(r.key3) as SubCount,   CASE WHEN DATEPART(hour, ra.action_time) >= 12 THEN  CONVERT(DATETIME, CONVERT(VARCHAR, ra.action_time, 102), 102)
                ELSE   CONVERT(DATETIME, CONVERT(VARCHAR, DATEADD(Day, -1,ra.action_time), 102), 102)  
             END   AS ShiftDate, SUBSTRING(CONVERT(char(12),ra.action_time,108),1,2) as Hour, count(*) as RecordCount

So you want to count the rows? In this case count(*) as SubCount would be better.

Can you provide the rest of the SQL statement and an expected result?
0
 

Author Comment

by:Scotto13
ID: 17076524
jrb1,

you got exactly what I needed.
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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

789 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