P粉1477476372023-07-25 10:55:52
Please add HAVING condition to your query:
SELECT user_id, city, COUNT(*) as result FROM visits WHERE start_ts >= 1675209600 -- 1675209600 = 01.02.2023 00:00 AND end_ts <= 1676419200 -- 1676419200 = 15.2.2023 00:00 GROUP BY user_id, city HAVING result = 1
If you need to count only the number of unique cities, you can use COUNT(DISTINCT(city)) and remove them from the grouping like this:
SELECT user_id, city, COUNT(DISTINCT(city)) as result FROM visits WHERE start_ts >= 1675209600 -- 1675209600 = 01.02.2023 00:00 AND end_ts <= 1676419200 -- 1676419200 = 15.2.2023 00:00 GROUP BY user_id HAVING result = 1