Solved

Update to SQL - Count Values of Zero

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

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

733 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