INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How to return all rows on a SUM/COUNT query?

How to return all rows on a SUM/COUNT query?

(OP)
I have a query that simply counts how many items exist for a particular status within a given data range. The query works...

CODE

SELECT DATE(MY_TIMESTAMP) AS DATE,
SUM(CASE WHEN STATUS_ID=10 THEN 1 ELSE 0 END) AS STATUS10,
SUM(CASE WHEN STATUS_ID=20 THEN 1 ELSE 0 END) AS STATUS20
FROM "MYDBB"."TABLE0"
WHERE
DATE(TIMESTAMP) >= DATE('2011-12-11') AND
DATE(TIMESTAMP) <= DATE('2011-12-17')
GROUP BY DATE(MY_TIMESTAMP);

The sorta problem is that it only returns rows for a given date where at least one of the counts exists. Can I get it to return all dates within the range provided with values of 0, 0?

RE: How to return all rows on a SUM/COUNT query?



hi,

Something like...

CODE

WHERE
(
  (
      DATE(TIMESTAMP) >= DATE('2011-12-11')
  AND DATE(TIMESTAMP) <= DATE('2011-12-17')
  )
   OR TIMESTAMP Is Null
)

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: How to return all rows on a SUM/COUNT query?

(OP)
Hi, thanks for the reply. That won't work because the TIMESTAMP field isn't the NULL item in question. The TIMESTAMP is never NULL. What might not exist within a date range is something with the STATUS_ID of 10 or 20. There are many statuses. So, for those dates without an item with the STATUS_ID of 10 or 20 no row is returned. I would like...

2011-12-11  0   0

However, what is returned is no row at all.

RE: How to return all rows on a SUM/COUNT query?

CODE

SELECT DATE(MY_TIMESTAMP) AS DATE,
SUM(CASE WHEN STATUS_ID=10 THEN 1 ELSE 0 END) AS STATUS10,
SUM(CASE WHEN STATUS_ID=20 THEN 1 ELSE 0 END) AS STATUS20
FROM "MYDBB"."TABLE0"
WHERE
DATE(TIMESTAMP) >= DATE('2011-12-11') AND
DATE(TIMESTAMP) <= DATE('2011-12-17')
GROUP BY DATE(MY_TIMESTAMP)
UNION ALL
SELECT DATE(MY_TIMESTAMP) AS DATE,
0 AS STATUS10,
0 AS STATUS20
FROM "MYDBB"."TABLE0"
WHERE
DATE(TIMESTAMP) >= DATE('2011-12-11') AND
DATE(TIMESTAMP) <= DATE('2011-12-17') and
STATUS_ID NOT IN (10,20)

Ties Blom
 
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close