【问题标题】:postgresql databasePostgreSQL 数据库
【发布时间】:2009-09-15 12:19:25
【问题描述】:

我想通过使用 postgresql 数据库进行查询,选择具有相同用户名和相同创建日期的用户

【问题讨论】:

  • 抱歉,您提供的信息太少,甚至无法开始回答问题。从您的数据库架构的详细信息开始...

标签: sql postgresql


【解决方案1】:

这样的事情应该可以解决问题。这将返回任何用户/小时对以及计数(未经测试):

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

【讨论】:

  • 我猜“同一小时的创建日期”是指同一小时内的同一绝对时间。所以 date_trunc('hour', users.created_at) 将是选择的函数。
  • @incredible_Honk,绝对。使用 date_trunc 而不是 datepart。
【解决方案2】:
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;

应该很好用。

【讨论】:

  • 感谢您的帮助,它工作正常,我想添加一个约束,即创建日期 = 今天谢谢
猜你喜欢
  • 1970-01-01
  • 2015-12-18
  • 2014-07-29
  • 2011-06-23
  • 2016-12-07
  • 2018-08-22
  • 2012-05-22
  • 2011-10-08
  • 2011-08-09
相关资源
最近更新 更多