SELECT u.*
FROM users AS u
JOIN visits AS v ON u.id = v.user_id
WHERE ST_Within(u.location, ST_GeomFromText('POLYGON((2.25 48.82, 2.41 48.82, 2.41 48.90, 2.25 48.90, 2.25 48.82))'))
AND v.location <@ ST_MakeEnvelope(3.559, 41.303, 7.559, 46.235)
AND v.visit_date >= (CURRENT_DATE - INTERVAL '1 month')
- This SQL statement assumes that there are two tables:
users
and visits
. The users
table has columns id
and location
(which stores lat/lng as a geometry type). The visits
table has columns user_id
, location
(also a geometry type), and visit_date
.
- The
ST_Within
function is used to find users who live in Paris. The coordinates for the polygon are roughly around Paris.
- The
ST_MakeEnvelope
function is used to create a bounding box that roughly covers the south of France. The @<
operator checks if the location
of the visit is within this bounding box. The date of the visit is checked to be within the last month.