[ACCEPTED]-MySQL: Select All Dates In a Range Even If No Records Present-gaps-and-islands

Accepted answer
Score: 19

I hope you will figure out the rest.

select  * from (
select date_add('2003-01-01 00:00:00.000', INTERVAL n5.num*10000+n4.num*1000+n3.num*100+n2.num*10+n1.num DAY ) as date from
(select 0 as num
   union all select 1
   union all select 2
   union all select 3
   union all select 4
   union all select 5
   union all select 6
   union all select 7
   union all select 8
   union all select 9) n1,
(select 0 as num
   union all select 1
   union all select 2
   union all select 3
   union all select 4
   union all select 5
   union all select 6
   union all select 7
   union all select 8
   union all select 9) n2,
(select 0 as num
   union all select 1
   union all select 2
   union all select 3
   union all select 4
   union all select 5
   union all select 6
   union all select 7
   union all select 8
   union all select 9) n3,
(select 0 as num
   union all select 1
   union all select 2
   union all select 3
   union all select 4
   union all select 5
   union all select 6
   union all select 7
   union all select 8
   union all select 9) n4,
(select 0 as num
   union all select 1
   union all select 2
   union all select 3
   union all select 4
   union all select 5
   union all select 6
   union all select 7
   union all select 8
   union all select 9) n5
) a
where date >'2011-01-02 00:00:00.000' and date < NOW()
order by date

With 7

select n3.num*100+n2.num*10+n1.num as date

you will get a column with numbers from 6 0 to max(n3)*100+max(n2)*10+max(n1)

Since 5 here we have max n3 as 3, SELECT will return 4 399, plus 0 -> 400 records (dates in calendar).

You 3 can tune your dynamic calendar by limiting 2 it, for example, from min(date) you have 1 to now().

Score: 6

This question asks the same thing I think. Generally 6 the accepted answer seems to be that you 5 either do it in your application logic (read 4 in what you have into an array, then loop 3 through the array and create the missing 2 dates), or you use temporary tables filled 1 with the dates you wish to join.

Score: 6

This is better to do as:

-- 7 Days:
set @n:=date(now() + interval 1 day);
SELECT qb.day_series as days , COALESCE(col_byte, 0) as Bytes from tbl1 qa
    right join (
        select (select @n:= @n - interval 1 day) day_series from tbl1 limit 7 ) as qb 
    on date(qa.Timestamp) = qb.day_series and 
qa.Timestamp > DATE_SUB(curdate(), INTERVAL 7 day) order by qb.day_series asc

-- 30 Days:
set @n:=date(now() + interval 1 day);
SELECT qb.day_series as days , COALESCE(col_byte, 0) as Bytes from tbl1 qa
    right join (
        select (select @n:= @n - interval 1 day) day_series from tbl1 limit 30 ) as qb 
    on date(qa.Timestamp) = qb.day_series and 
qa.Timestamp > DATE_SUB(curdate(), INTERVAL 30 day) order by qb.day_series asc;

or without variable 1 like this:

SELECT qb.day_series as days , COALESCE(col_byte, 0) as Bytes from tbl1 qa
right join (
    select curdate() - INTERVAL a.a day as day_series from(
        select 0 as a union all select 1 union all select 2 union all 
        select 3 union all select 4 union all 
        select 5 union all select 6 union all select 7
    ) as a ) as qb
on date(qa.Timestamp) = qb.day_series and
qa.Timestamp > DATE_SUB(curdate(), INTERVAL 7 day) order by qb.day_series asc;
Score: 1

Do a right outer join to a table, call it 3 tblCalendar, that is pre-populated with 2 the dates you wish to report on. And join 1 on the date field.

Paul

Score: 1

Query is:

SELECT qb.dy as yourday, COALESCE(count(yourcolumn), 0) as yourcount from yourtable qa 
right join (
    select curdate() as dy    union
    select DATE_SUB(curdate(), INTERVAL 1 day) as dy     union
    select DATE_SUB(curdate(), INTERVAL 2 day) as dy     union
    select DATE_SUB(curdate(), INTERVAL 3 day) as dy     union
    select DATE_SUB(curdate(), INTERVAL 4 day) as dy     union
    select DATE_SUB(curdate(), INTERVAL 5 day) as dy     union
    select DATE_SUB(curdate(), INTERVAL 6 day) as dy        
    ) as qb 
on qa.dates = qb.dy 
and qa.dates > DATE_SUB(curdate(), INTERVAL 7 day)
order by qb.dy asc;

and the result is:

+------------+-----------+
| yourday    | yourcount |
+------------+-----------+
| 2015-06-24 | 274339    |
| 2015-06-25 |      0    |
| 2015-06-26 |      0    |
| 2015-06-27 |      0    |
| 2015-06-28 | 134703    |
| 2015-06-29 |  87613    |
| 2015-06-30 |      0    |
+------------+-----------+

0

Score: 0

On further thought, something like this 4 should be what you want:

CREATE TEMPORARY TABLE DateSummary1 ( datenew timestamp ) SELECT DISTINCT(DATE(datecreated)) as datenew FROM users;

CREATE TEMPORARY TABLE DateSummary2 ( datenew timestamp, number int ) SELECT DATE(datecreated) as datenew, count(*) AS number FROM users 
WHERE DATE(datecreated) > '2009-06-21' AND DATE(datecreated) <= DATE(NOW())
GROUP BY DATE(datecreated) ORDER BY datecreated ASC;

SELECT ds1.datenew,ds2.number FROM DateSummary1 ds1 LEFT JOIN DateSummary2 ds2 on ds1.datenew=ds2.datenew;

This gives you all 3 the dates in the first table, and the count summary 2 data in the second table. You might need 1 to replace ds2.number with IF(ISNULL(ds2.number),0,ds2.number) or something similar.

More Related questions