2017-10-19 82 views
1

我需要獲取每個人使用PostGIS以多邊形形式定義的特定位置的次數。獲取動作發生的次數

這裏是我開始的SQL語句:

SELECT id_p 
FROM Movement 
WHERE ST_Contains('POLYGON((long1 lat1, long2 lat2, long3 lat3, long4 lat4))', 
        ST_MakePoint(longitude,latitude)) 
    AND (datehour::date BETWEEN '2017-01-01' AND '2017-01-31') 
    AND speed = 0 

enter image description here

回答

2

你可以嘗試做一個基本的數查詢:

SELECT 
    id_p, 
    COUNT(*) AS cnt 
FROM 
    Movement 
WHERE 
    ST_Contains('POLYGON((long1 lat1, long2 lat2, long3 lat3, long4 lat4))', 
       ST_MakePoint(longitude,latitude)) AND 
    datehour::date BETWEEN '2017-01-01' AND '2017-01-31' AND 
    speed = 0 
GROUP BY 
    id_p; 

爲了加快這個查詢,你可以考慮加入WHERE條款中使用的列的索引,即datehourspeed。您還可以考慮調用ST_Contains時涉及的列的地理空間索引。