怎么让计数出现在一周

时间:2013-08-29 10:21:50

标签: mysql sql

该表显示了车牌号,显示了相机的时间,位置。我需要找到每周出现不止一次的汽车,我需要知道他们是谁以及每周的显示频率,以及满足条件的汽车总数。

我试图手动分割时间段,但效率太低。 我尝试了以下查询:

   SELECT *, 
       Variance(Time_to_sec(time)), 
       Count(*) 
   FROM   trafficdata.anpr_in 
   WHERE  location = 'a35.1.ob.1' 
       AND Date(time) BETWEEN '2012-05-09'AND '2012-05-15' 
       AND Time(time) BETWEEN '07:00:00' AND '07:05:00' 
   GROUP  BY plate 
   HAVING ( Count(plate) > 3 ); 

输入表如下所示:

  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

2 个答案:

答案 0 :(得分:1)

如果你的表是

create table tickets (car_number varchar(10),date_of_offense date, 
camera_number varchar(10));

然后

select car_number from tickets group by week(date_of_offense ) having count(*)>1

会让你那些一周内冒犯过两次或更多次的汽车

答案 1 :(得分:1)

当你说“我需要找到每周出现不止一次的汽车”时,你的意思是:一周内不止一次出现的汽车?或者,在此期间每周出现的汽车不止一次?

第一个可以回答为:

SELECT plate, week(time), count(*)
FROM   trafficdata.anpr_in 
WHERE  location = 'a35.1.ob.1' AND
       Date(time) BETWEEN'2012-05-09'AND '2012-05-15' AND
       Time(time) BETWEEN '07:00:00' AND '07:05:00' 
GROUP  BY plate , week(time)
HAVING count(*) > 1;

第二个可以回答为:

select plate
from (SELECT plate, week(time), count(*) as cnt
      FROM   trafficdata.anpr_in 
      WHERE  location = 'a35.1.ob.1' AND
             Date(time) BETWEEN'2012-05-09'AND '2012-05-15' AND
             Time(time) BETWEEN '07:00:00' AND '07:05:00' 
      GROUP  BY plate , week(time)
      HAVING count(*) > 1
     ) t
having min(plate) > 1;