如何在特定时间段内获取计数

时间:2013-08-28 20:29:04

标签: mysql

我需要计算每周出现10次以上的车辆数量,而这些车辆只出现在2012-07-24'之间。和' 2012-09-02'和。 我试过了:

select distinct * from anpr_in where location='a35.1.ob.1' and date(time) 
between '2012-07-24'and '2012-09-02' group by plate having count(plate)>10;

但它不会过滤那些仍然出现在期间的汽车。

表格如下:

plate           location       number    time
T971JUR     A3024.7.IB.1         96     2012-05-13 18:06:17
HN52YWE     A3024.13.OB.1        94     2012-05-13 18:09:53
R179NBM     A335.6.OB.1          90     2012-05-13 18:08:55
WV07EAX     A35.1.IB.1           91     2012-05-13 18:05:09
HF02NFH     A334.14.IB.1         94     2012-05-13 18:06:43
HK11BHE     A33.5.IB.2           96     2012-05-13 18:07:52
39341       A35.1.OB.1           0      2012-05-13 18:08:09
CP61PCZ     A35.1.IB.1           96     2012-05-13 18:07:04
LM06UKN     A3024.8.IB.1         96     2012-05-13 18:06:44
3B001       A35.1.OB.1           0      2012-05-13 18:07:49
WG10YCT     A3024.13.OB.1        90     2012-05-13 18:08:04
HY60XTE     A3024.8.IB.1         93     2012-05-13 18:05:31
M397BLA     A334.14.OB.1         92     2012-05-13 18:06:57

1 个答案:

答案 0 :(得分:1)

这应该返回符合您标准的印版数量

SELECT COUNT(*) plate_count
FROM (SELECT plate
      FROM anpr_in
      WHERE location='a35.1.ob.1'
      AND DATE(time) BETWEEN '2012-07-24' AND '2012-09-02'
      GROUP BY plate
      HAVING COUNT(*) > 10) x

SQLFIDDLE

您不需要DISTINCT,因为GROUP BY已经将其缩减为不同的牌号。