PostgreSQL / PostGIS计算访问次数

时间:2017-10-20 10:38:10

标签: sql postgresql postgis

我需要计算车辆在两个日期之间访问某个地点的次数,考虑到每天最大访问量为1,即使车辆每天多次访问该地点。

enter image description here

这是我写的:

SELECT DISTINCT id_vehicle, datehour::date as dt, COUNT(id_vehicle) AS times 
FROM history
WHERE datehour between '2017-01-01' AND CURRENT_TIMESTAMP AND
      ST_Contains('POLYGON((x1 y1, x2 y2, x3 y3, x4 y4, x1 y1))',ST_MakePoint(longitude,latitude))
GROUP BY datehour::date, id_vehicle;

我需要的结果:

enter image description here

1 个答案:

答案 0 :(得分:2)

如果我理解正确,你想:

SELECT id_vehicle,  COUNT(DISTINCT datehour::date) AS times 
FROM history
WHERE datehour between '2017-01-01' AND CURRENT_TIMESTAMP AND
      ST_Contains('POLYGON((x1 y1, x2 y2, x3 y3, x4 y4, x1 y1))', ST_MakePoint(longitude, latitude))
GROUP BY id_vehicle;