Selecting info from users who have been inactive for at least a week I need to find the users who have not done a task in at least a week, and pull the relevant information from them. In this case, their id, names, and data1 from another table. This is what I have so far, but it's not working. Something to do with the joins I think? 

Can someone help me out? :)

Additional info: the user_identity table is a bit complicated, with multiple rows with the same userid hence the distinct.

SELECT DISTINCT ON (u.id) u.id, u.given_name, u.family_name, i.data1
                FROM users u JOIN task t_week on u.id = t_week.user_id    
                and t_week.created_at &gt; %s JOIN user_identity i on (u.id=i.user_id)
                WHERE t_week.created_at IS NULL AND i.content_type='email_address' AND i.owner_id is NULL
                GROUP BY u.id, u.given_name, u.family_name, i.data1,i.created_at ORDER BY u.id, i.created_at
Assuming the date you are referring to is t_week.created_at, not i.created_at:

select * from (
    select DISTINCT ON (u.id) u.id, t_week.created_at, ... 
        FROM users u JOIN task t_week on u.id = t_week.user_id JOIN...
        ORDER by u.id, t_week.created_at desc
) foo 
WHERE t_week.created_at &lt; %s
ORDER BY u.id, i.created_at


You have to use the sub-select because DISTINCT ON doesn't support a HAVING clause, so you wrap it in a sub-select and use a WHERE clause on the outside.

This would not find people have never done a task all.  To do that you would need to do a left join to task table, then 

WHERE t_week.created_at &lt; %s or t_week.created_at is null