【发布时间】:2020-11-02 01:45:38
【问题描述】:
我正在处理一个查询,该查询返回给定日期的每小时时间序列,但我需要按另一个表上的特定列进行过滤,在我的例子中是用户 ID。
这是我当前的查询,它返回当天每个小时的提交计数:
SELECT hours, count(s.id)
FROM generate_series(current_date, current_date + interval '23h', cast('1h' as interval)) hours
left join submission s on hours = date_trunc('hour', s.submitted_date)
group by hours
order by hours;
但是,这些记录与用户 ID 无关,因此当我使用这样的查询按用户 ID 过滤时,它只会返回收到该用户提交的小时数,而我需要它返回每个用户的记录小时很像上面的查询:
SELECT hours, count(s.id)
FROM generate_series(current_date, current_date + interval '23h', cast('1h' as interval)) hours
left join submission s on hours = date_trunc('hour', s.submitted_date)
left join form f on s.form_custom_id = f.custom_id
left join "user" u on f.user_id = u.id
where u.id = 4
group by hours
order by hours;
这是我的 SQL Fiddle:http://www.sqlfiddle.com/#!17/a8d80/1
任何帮助将不胜感激!
【问题讨论】:
-
请use text, not images/links, for text--including tables & ERDs。仅将图像用于无法表达为文本或增强文本的内容。在图像中包含图例/键和说明。将需要询问的内容放在您的帖子中,而不仅仅是在链接/小提琴中。 minimal reproducible example 包括剪切 & 粘贴 & 可运行代码,包括作为代码输入的最小代表性示例;期望和实际输出(包括逐字错误消息);标签和版本;明确的规范和解释。说明您可以做什么以及它们与您的目标有何关系。
标签: sql postgresql date time-series left-join