Solved

Update to SQL - Count Values of Zero

Posted on 2006-07-10
4
366 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.

809 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