I have the database consists of 4 tables:
- users(id, "name", surname, birthdate)
- friendships(userid1, userid2, "timestamp")
- posts(id, userid, "text", "timestamp")
- likes(postid, userid, "timestamp")
I need to get a result set of unique usernames having more than 3 friendships within January of 2018 and their "likes" average per "post" in the range of [10; 35).
I wrote this statement for the first step:
select distinct u."name"
from users u
join friendships f on u.id = f.userid1
where f."timestamp" between '2018-01-01'::timestamp and '2018-01-31'::timestamp
group by u.id
having count(f.userid1) > 3;
It's working fine and returns 3 rows. But when I'm adding the second part this way:
select distinct u."name"
from users u
join friendships f on u.id = f.userid1
join posts p on p.userid = u.id
join likes l on p.id = l.postid
where f."timestamp" between '2018-01-01'::timestamp and '2018-01-31'::timestamp
group by u.id
having count(f.userid1) > 3
and ((count(l.postid) / count(distinct l.postid)) >= 10
and (count(l.postid) / count(distinct l.postid)) < 35);
I'm getting crazy 94 rows. I don't know why. Will be thankful for possible help.