dbTalk Databases Forums  

How to return 0 count for empty date ranges

comp.databases.mysql comp.databases.mysql


Discuss How to return 0 count for empty date ranges in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
John R
 
Posts: n/a

Default How to return 0 count for empty date ranges - 02-06-2010 , 03:57 PM






Hi everyone...

I am running this query which grabs activity per day for 10 days

SELECT DATE_FORMAT( created, "%c/%d/%Y" ) AS `day` , COUNT( id ) AS
'activity'
FROM `tracking`
WHERE url_id = 1
GROUP BY DAY( created )
LIMIT 0 , 10

Which successfully returns activity by day:

day activity
1/26/2010 5
2/01/2010 1
2/02/2010 1
2/03/2010 1
2/05/2010 8
2/06/2010 5

However, I am trying to get it to return "0"s for empty days, such as:

day activity
1/26/2010 5
1/27/2010 0
1/28/2010 0
1/29/2010 0
1/30/2010 0
2/01/2010 1
2/02/2010 1
2/03/2010 1
2/05/2010 8
2/06/2010 5

Thanks in advance!

Reply With Quote
  #2  
Old   
Lennart
 
Posts: n/a

Default Re: How to return 0 count for empty date ranges - 02-06-2010 , 04:11 PM






On 6 Feb, 21:57, John R <john... (AT) gmail (DOT) com> wrote:
Quote:
Hi everyone...

I am running this query which grabs activity per day for 10 days

SELECT DATE_FORMAT( created, *"%c/%d/%Y" ) AS *`day` , COUNT( id ) AS
'activity'
FROM *`tracking`
WHERE url_id = 1
GROUP BY DAY( created )
LIMIT 0 , 10

Which successfully returns activity by day:

day * * activity
1/26/2010 * * * 5
2/01/2010 * * * 1
2/02/2010 * * * 1
2/03/2010 * * * 1
2/05/2010 * * * 8
2/06/2010 * * * 5

However, I am trying to get it to return "0"s for empty days, such as:

day * * activity
1/26/2010 * * * 5
1/27/2010 0
1/28/2010 0
1/29/2010 0
1/30/2010 0
2/01/2010 * * * 1
2/02/2010 * * * 1
2/03/2010 * * * 1
2/05/2010 * * * 8
2/06/2010 * * * 5

Thanks in advance!
http://tinyurl.com/yc6fz4h

/Lennart

Reply With Quote
  #3  
Old   
John R
 
Posts: n/a

Default Re: How to return 0 count for empty date ranges - 02-06-2010 , 05:25 PM



On Feb 6, 3:11*pm, Lennart <erik.lennart.jons... (AT) gmail (DOT) com> wrote:
Quote:
On 6 Feb, 21:57, John R <john... (AT) gmail (DOT) com> wrote:





Hi everyone...

I am running this query which grabs activity per day for 10 days

SELECT DATE_FORMAT( created, *"%c/%d/%Y" ) AS *`day` , COUNT( id ) AS
'activity'
FROM *`tracking`
WHERE url_id = 1
GROUP BY DAY( created )
LIMIT 0 , 10

Which successfully returns activity by day:

day * * activity
1/26/2010 * * * 5
2/01/2010 * * * 1
2/02/2010 * * * 1
2/03/2010 * * * 1
2/05/2010 * * * 8
2/06/2010 * * * 5

However, I am trying to get it to return "0"s for empty days, such as:

day * * activity
1/26/2010 * * * 5
1/27/2010 0
1/28/2010 0
1/29/2010 0
1/30/2010 0
2/01/2010 * * * 1
2/02/2010 * * * 1
2/03/2010 * * * 1
2/05/2010 * * * 8
2/06/2010 * * * 5

Thanks in advance!

http://tinyurl.com/yc6fz4h

/Lennart
I have seen that method elsewhere, however I am still a bit confused
on what I would need. A massive table of dates from now to the
foreseeable future? Or a constantly changing table that just has the
most recent 10 days dates?

Reply With Quote
  #4  
Old   
Derek Turner
 
Posts: n/a

Default Re: How to return 0 count for empty date ranges - 02-07-2010 , 07:28 AM



On Sat, 06 Feb 2010 14:25:49 -0800, John R wrote:

Quote:
On Feb 6, 3:11Â*pm, Lennart <erik.lennart.jons... (AT) gmail (DOT) com> wrote:
On 6 Feb, 21:57, John R <john... (AT) gmail (DOT) com> wrote:





Hi everyone...

I am running this query which grabs activity per day for 10 days

SELECT DATE_FORMAT( created, Â*"%c/%d/%Y" ) AS Â*`day` , COUNT( id ) AS
'activity'
FROM Â*`tracking`
WHERE url_id = 1
GROUP BY DAY( created )
LIMIT 0 , 10

Which successfully returns activity by day:

day Â* Â* activity
1/26/2010 Â* Â* Â* 5
2/01/2010 Â* Â* Â* 1
2/02/2010 Â* Â* Â* 1
2/03/2010 Â* Â* Â* 1
2/05/2010 Â* Â* Â* 8
2/06/2010 Â* Â* Â* 5

However, I am trying to get it to return "0"s for empty days, such
as:

day Â* Â* activity
1/26/2010 Â* Â* Â* 5
1/27/2010 0
1/28/2010 0
1/29/2010 0
1/30/2010 0
2/01/2010 Â* Â* Â* 1
2/02/2010 Â* Â* Â* 1
2/03/2010 Â* Â* Â* 1
2/05/2010 Â* Â* Â* 8
2/06/2010 Â* Â* Â* 5

Thanks in advance!

http://tinyurl.com/yc6fz4h

/Lennart

I have seen that method elsewhere, however I am still a bit confused on
what I would need. A massive table of dates from now to the foreseeable
future? Or a constantly changing table that just has the most recent 10
days dates?
As I read it, a table with 31 rows containing the numbers 1 to 31. It's
only necessary to cover all the possible days of the month.

Reply With Quote
  #5  
Old   
John R
 
Posts: n/a

Default Re: How to return 0 count for empty date ranges - 02-08-2010 , 11:09 AM



On Feb 7, 6:28*am, Derek Turner <frde... (AT) cesmail (DOT) net> wrote:
Quote:
On Sat, 06 Feb 2010 14:25:49 -0800, John R wrote:
On Feb 6, 3:11*pm, Lennart <erik.lennart.jons... (AT) gmail (DOT) com> wrote:
On 6 Feb, 21:57, John R <john... (AT) gmail (DOT) com> wrote:

Hi everyone...

I am running this query which grabs activity per day for 10 days

SELECT DATE_FORMAT( created, *"%c/%d/%Y" ) AS *`day` , COUNT( id) AS
'activity'
FROM *`tracking`
WHERE url_id = 1
GROUP BY DAY( created )
LIMIT 0 , 10

Which successfully returns activity by day:

day * * activity
1/26/2010 * * * 5
2/01/2010 * * * 1
2/02/2010 * * * 1
2/03/2010 * * * 1
2/05/2010 * * * 8
2/06/2010 * * * 5

However, I am trying to get it to return "0"s for empty days, such
as:

day * * activity
1/26/2010 * * * 5
1/27/2010 0
1/28/2010 0
1/29/2010 0
1/30/2010 0
2/01/2010 * * * 1
2/02/2010 * * * 1
2/03/2010 * * * 1
2/05/2010 * * * 8
2/06/2010 * * * 5

Thanks in advance!

http://tinyurl.com/yc6fz4h

/Lennart

I have seen that method elsewhere, however I am still a bit confused on
what I would need. A massive table of dates from now to the foreseeable
future? Or a constantly changing table that just has the most recent 10
days dates?

As I read it, a table with 31 rows containing the numbers 1 to 31. It's
only necessary to cover all the possible days of the month.
Thanks for the response. I still don't understand how this would work.
My dates are stored in the DATETIME format. If I have a list of 1-31,
how would I just match the last 10 days? Wouldn't you need the month
and even the year?

Reply With Quote
  #6  
Old   
Derek Turner
 
Posts: n/a

Default Re: How to return 0 count for empty date ranges - 02-09-2010 , 04:44 AM



On Mon, 08 Feb 2010 08:09:01 -0800, John R wrote:


Quote:
Thanks for the response. I still don't understand how this would work.
My dates are stored in the DATETIME format. If I have a list of 1-31,
how would I just match the last 10 days? Wouldn't you need the month and
even the year?
No, they're not. They are stored as the number of seconds since 1 January
1970. MySQL coverts that to the DATETIME format when you query the data
base. There are a number of functions in MySQL that allow you to get the
data out in whatever format you want. e.g.

SELECT UNIX_TIMESTAMP(eventDate) AS unixdate, ....

by using these in a complex query you can use an outside join on the 31-
row table to return your null values. That's how I read it, but I've
never done it!

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.