2017-10-20 57 views
0

考慮到每天最多訪問次數爲1,即使車輛每天多次訪問該地點,我也需要統計車輛訪問兩個日期之間的地點的次數。PostgreSQL/PostGIS計數訪問次數

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

回答

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;