我的目的是对在某些日期(第0天到第7天以及第0天到第45天)之间多次发生的行进行计数。代码在下面,因此更容易理解我的意思:
g
毫无疑问,只有以下COUNT个可以使用。试图创建一个子查询,但失败或返回一些废话。
感谢您的帮助!
答案 0 :(得分:0)
使用Case When
表达式
SELECT
d.personal_id,
COUNT (case when d.delivered < d.created + 7 * interval '1 day' then d.deliveryID end) as Deliveries_7D,
COUNT (case when d.delivered < d.created + 45 * interval '1 day' then d.deliveryID end) as Deliveries_45D
FROM table_d d
WHERE d.delivered < d.created + 45 * interval '1 day'
GROUP BY d.personal_id
LIMIT 20
答案 1 :(得分:0)
您可以通过这种方式使用子查询
select t1.personal_id, t1.Deliveries_7D, t2.Deliveries_45D
from (
select d.personal_id COUNT (d.deliveryID) as Deliveries_7D
FROM table_d d
WHERE d.delivered < d.created + 7 * interval '1 day'
GROUP BY d.personal_id
) t1
inner join (
select d.personal_id COUNT (d.deliveryID) as Deliveries_45D
FROM table_d d
WHERE d.delivered < d.created + 45 * interval '1 day'
GROUP BY d.personal_id
) t2. on t1.personal_id = t2.personal_id