Consider the following tables
=# \d users
Column | Type
--------+-----------------------
id | integer
name | character varying(32)
=# \d profiles
Column | Type
---------+---------
id | integer
user_id | integer
=# \d views
Column | Type
------------+-----------------------------
id | integer
profile_id | integer
time | timestamp without time zone
I need to find all users with an associated view in each month of a given date range. Currently I am doing the following:
with months as (
select to_char(month, 'MM/YYYY') from generate_series('2014-07-01', '2014-09-01', INTERVAL '1 month') as month
)
select * from users
join profiles on user_id = users.id
join views on profile_id = profiles.id
and to_char(views.time, 'MM/YYYY') in (select * from months)
I have setup a fiddle here.
Currently the results include the user Kyle who had no views in August and September. The correct result should only include the user Stan who had views in all the 3 months in the given range. How do we modify this query to return the desired result?