0

I need the count of all dates including the nonexistent

SELECT ifnull(COUNT(*),0) as num , date_format(c.dataCupo,"%d/%m/%Y") as data FROM cupons c WHERE c.dataCupo between "2017-02-02" AND "2018-05-04" AND c.proveidor!="VINCULADO" and c.empresa=1 group by date_format(c.dataCupo,"%Y-%m-%d")

//And I need to count all months including the nonexistent

SELECT ifnull(COUNT(*),0) as num , date_format(c.dataCupo,"%m/%Y") as data
FROM cupons c 
WHERE c.dataCupo between "2017-02-02" AND "2018-05-04" AND c.proveidor!="VINCULADO" and c.empresa=1
group by date_format(c.dataCupo,"%Y-%m")

//And I need to count of all years including the nonexistent

SELECT ifnull(COUNT(*),0) as num , date_format(c.dataCupo,"%Y") as data
FROM cupons c 
WHERE c.dataCupo between "2015-02-02" AND "2018-05-04" AND c.proveidor!="VINCULADO" and c.empresa=1
group by date_format(c.dataCupo,"%Y")

The result i want its:

02/02/2017 | 10

03/02/2017 | 0

04/02/2017 | 2

05/02/2017 | 0 ....

AND

02/2017 | 50

03/2017 | 0

04/2017 | 10

AND

2015 | 0

2016 | 10

2017 | 15

2018 | 0

Claudio
  • 10,614
  • 4
  • 31
  • 71
  • Where is the sample data? – cdaiga May 04 '18 at 11:18
  • 02/02/2017 | 10 03/02/2017 | 0 04/02/2017 | 2 05/02/2017 | 0 .... AND 02/2017 | 50 03/2017 | 0 04/2017 | 10 AND 2015 | 0 2016 | 10 2017 | 15 2018 | 0 – sergi castells May 04 '18 at 11:19
  • Possible duplicate of [MySQL: Select All Dates In a Range Even If No Records Present](https://stackoverflow.com/questions/1046865/mysql-select-all-dates-in-a-range-even-if-no-records-present) – Nick May 04 '18 at 11:43

2 Answers2

0

Easiest way to do this is with a Calendar table. This table will have a datetime column that you can join to and is really useful for reporting. Here goes an example of how to make one in MySQL.

https://gist.github.com/bryhal/4129042

Now that you have the Calendar table, you can join to it to find counts of all dates in a date range.

All days example:

select num, td.db_date
FROM 
time_dimension td 
left join 
   (SELECT ifnull(COUNT(*),0) as num , c.dataCupo as data
   FROM cupons c 
   WHERE c.dataCupo between "2017-02-02" AND "2018-05-04" AND 
      c.proveidor!="VINCULADO" and c.empresa=1
   group by c.dataCupo) t
on t.data  = td.db_date
WHERE td.db_date between "2017-02-02" AND "2018-05-04" 

All months example:

select 
    sum(t.num),
    CONCAT(month(td.db_date),"-",year(td.db_date))
FROM 
time_dimension td 
left join 
   (SELECT 
       ifnull(COUNT(*),0) as num , 
       c.dataCupo as data
   FROM cupons c 
   WHERE c.dataCupo between "2017-02-02" AND "2018-05-04" AND 
      c.proveidor!="VINCULADO" and c.empresa=1) t

on c.data = t.data 
WHERE td.db_date between "2017-02-02" AND "2018-05-04" 
group by CONCAT(month(td.db_date),"-",year(td.db_date))
Skeik
  • 51
  • 1
  • 4
0

You should create A Temporary Table To Store All The Date Ranges Between Your Date Ranges

    CREATE TEMPORARY TABLE IF NOT EXISTS AllDateRange engine=memory 
 SELECT DATE(cal.date) Date
 FROM ( 
  SELECT  ( case when  @prmToDate = @prmFromDate then @prmFromDate else

  SUBDATE( @prmFromDate, INTERVAL  (DATEDIFF(@prmToDate,@prmFromDate))  DAY) + INTERVAL xc DAY end ) AS Date 
  FROM ( 
        SELECT @xi:=@xi+1 as xc from 
        (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) xc1, 
        (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) xc2, 
        (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) xc3, 
        (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) xc4, 
        (SELECT @xi:=+1) xc0 
  ) xxc1 
   ) cal WHERE DATE( cal.date)  >=  DATE(@prmFromDate)  and DATE( cal.date) <= DATE(@prmToDate)    ;

And Then Join It With Your Table As.

SELECT   count(COALESCE(c.empresa, 0)) as num ,   date_format(a.Date,"%d/%m/%Y") as data from AllDateTimeRange a 
 left join cupons c  on a.Date=date_format(c.dataCupo,"%Y-%m-%d") 
 WHERE c.dataCupo between @prmFromDate AND @prmToDate  AND  c.proveidor!="VINCULADO" and c.empresa=1 
 group by date_format(c.dataCupo,"%Y-%m-%d");

Similarly Create Temp Tables For Month & Year And Then Join With Your Primary Table, as above in order to get your required results for month and year respectively.