鉴于以下架构:
用户
柱子 | 类型 |
---|---|
ID | 用户名 |
用户名 | 变量(255) |
条目
柱子 | 类型 |
---|---|
ID | 用户名 |
用户身份 | 用户名 |
插入_at | 时间戳 |
我将如何查询n
在两个日期之间每天创建条目的用户列表。例如,所有在 9 月 1 日至 9 月 15 日之间的 10 天内有条目的用户。
请注意,用户可能在一天内有多个条目。我对两个日期之间不同日期的条目感兴趣。
我正在使用 Postgres,但我想这是一个更广泛的 sql 问题。
select u.id as user_id, max(u.username), count(e.id) as entries_count
from users u
inner join (
select max(id) as id, user_id, max(inserted_at) as inserted_at
from entries
group by user_id cast(inserted_at as date)
) e on u.id = e.user_id
where e. inserted_at between {start_date} and {end_date}
group by u.id having count(e.id)>X
{start_date} => 时间范围的开始
{end_date} => 时间范围结束
X => 最少应该有多少条目
这里的时间范围包括在内
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句