i wanna to make a query that select users that have same username and same hour of creation date by using postgresql database
+1
A:
Something like this should do the trick. This will return any user/hour pair along with the count (untested):
select users.username, datepart('hour', users.created_at), count(*) from users
inner join users u2
on users.username = u2.username
and datepart('hour', users.created_at) = datepart('hour', u2.created_at)
group by users.username, datepart('hour', users.created_at) having count(*) > 1
hgimenez
2009-09-15 12:26:34
Thanks so much
Neveen
2009-09-15 12:37:17
I guess that 'same hour of creation date' is meant as same absolute time within the same hour. So date_trunc('hour', users.created_at) would be the function of choice.
Frank Bollack
2009-09-15 12:38:04
@incredible_Honk, absolutely. Use date_trunc instead of datepart.
hgimenez
2009-09-15 12:59:22
A:
select u.*
from users u
join (
select username, date_trunc('hour', creation_timestamp)
from users
group by 1, 2
having count(*) > 1
) as x on u.username = x.username
order by u.username;
Should work nicely.
depesz
2009-09-15 12:43:24
Thanks for your help it works fine and i wanna to add a constrains that is creation date = todayThanks
Neveen
2009-09-15 13:00:30