I have a table for persons and other for visits. I need to count the visits for the month only if it was the first one. So basically get a count of visits for the month but only if your visit for the month, was the first.
Example:
PEOPLE:
id
---------
20
30
23
VISITS
id | date
-------------------------
20 | 09-20-2019
20 | 10-01-2019
23 | 10-09-2019
30 | 10-07-2019
I want to know the coutning only if its the first one, on this example, the counting should equal to 2 because person with ID 20 had a past visit last month.
This is what I have so far on my query
SELECT * FROM visits
LEFT JOIN people ON visits.id = people.id
WHERE date_trunc('month', current_date) <= visits.visit_date AND visits.visit_date < date_trunc('month', current_date) + INTERVAL '1 month'
This is just giving me the visits for the month. How can I filter by only if the person doesnt have past visits.
count(distinct people.id)