日期间计数

93ze6v8z  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(358)

我需要所有日期的计数,包括不存在的日期 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") //我要数所有的月份,包括不存在的月份

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")

//我需要计算所有年份,包括不存在的年份

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")

我想要的结果是:
02/02/2017 | 10
03/02/2017 | 0
04/02/2017 | 2
05/02/2017 | 0 ....

02/2017 | 50
03/2017 | 0
04/2017 | 10
以及
2015 | 0
2016 | 10
2017 | 15
2018 | 0

fkaflof6

fkaflof61#

您应该创建一个临时表来存储日期范围之间的所有日期范围

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)    ;

然后把它和你的table连接起来。

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");

类似地,为月份和年份创建临时表,然后与主表联接,如上所述,以便分别获得月份和年份所需的结果。

x6h2sr28

x6h2sr282#

最简单的方法是使用日历表。这个表将有一个datetime列,您可以加入其中,它对于报告非常有用。下面是一个如何在mysql中创建一个的示例。
https://gist.github.com/bryhal/4129042
现在您已经有了日历表,可以加入它来查找某个日期范围内所有日期的计数。
全天示例:

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"

所有月份示例:

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))

相关问题