简体   繁体   English

如何在MySQL的无行日中每天对行数进行分组?

[英]How to group number of rows per day also in no-row days in MySQL?

So i have this table. 所以我有这张桌子。 It has some hundreds of rows. 它有几百行。 Every row has a datetime field in it. 每行中都有一个日期时间字段。 And what i need to acomplish is to get how much rows there are in given period of time but not for the whole period but for every day in this period. 我需要完成的是获取给定时间段内有多少行,而不是整个时间段,而是该时间段的每一天。 Till this point i know what to do. 到目前为止,我知道该怎么办。 But moreover i need to have also rows for the days that does not have any row in the table with value 0. 但是此外,我还需要在表中的行中没有值0的行。

So for example: 因此,例如:

2012-01-01 12:13
2012-01-01 43:32
2012-01-03 23:32

Should give me the result like this: 应该给我这样的结果:

2012-01-01 2
2012-01-02 0
2012-01-03 1

Anyone can help?? 任何人都可以帮忙吗?

To deal with dates with 0 corresponding records, my normal practice is to use a calendar table to join on. 为了处理带有0条对应记录的日期,我的常规做法是使用日历表进行联接。

For example, create a table with one field called calendar_date and populate it with every date from 1st Jan 2000 to 31st Dec 2070 , or some other range that suits your reporting purposes. 例如,使用一个名为calendar_date字段创建一个表,并用1st Jan 200031st Dec 2070 1st Jan 2000 31st Dec 2070每个日期或适合您报告目的的其他范围填充该表。

Then use something like... 然后使用类似...

SELECT
  calendar.calendar_date,
  COUNT(*)
FROM
  calendar
LEFT JOIN
  yourData
    ON  yourData.timeStamp >= calendar.calendar_date
    AND yourData.timeStamp <  calendar.calendar_date + 1
WHERE
      calendar.calendar_date >= '01 Jan 2012'
  AND calendar.calendar_date <  '04 Jan 2012'
GROUP BY
  calendar.calendar_date

This table can have many extra uses, such as flagging bank holidays, starts of weeks and months. 该表可以有许多其他用途,例如标记银行假期,几周和几个月的开始。 With prudent uses of flags and indexes, you can get a lot out of it. 通过谨慎使用标志和索引,您可以从中获得很多收益。

If you don't have a table with a list of dates in it, you can simulate one with something like the following query: 如果您没有其中包含日期列表的表,则可以使用类似以下查询的内容来模拟一个表:

select * from 
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) calendar_date from
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where calendar_date between ? /*start of date range*/ and ? /*end of date range*/

- so Dems' query could become: -因此Dems的查询可能变为:

SELECT
  calendar.calendar_date,
  COUNT(*)
FROM
  (select * from 
    (select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) calendar_date from
     (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
     (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
     (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
     (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
     (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
   WHERE calendar_date >= '01 Jan 2012'
     AND calendar_date <  '04 Jan 2012'
  ) calendar
LEFT JOIN
  yourData
    ON  yourData.timeStamp >= calendar.calendar_date
    AND yourData.timeStamp <  calendar.calendar_date + 1

try 尝试

SELECT DATE_FORMAT(YourDate, '%Y-%m-%d') as YourDate, COUNT(*)
FROM TableName where YourDate= 'date' 
GROUP BY YourDate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM