【发布时间】:2021-12-02 23:32:42
【问题描述】:
对于每个主题,我想在特定日期之后过滤他们的记录。但是,我想用作过滤器的日期对于每个主题都不同。有没有办法按主题分组,然后为每个组使用不同的有子句?
【问题讨论】:
-
与您的问题无关,但是:Postgres 9.1 是no longer supported,您应该尽快计划升级。
对于每个主题,我想在特定日期之后过滤他们的记录。但是,我想用作过滤器的日期对于每个主题都不同。有没有办法按主题分组,然后为每个组使用不同的有子句?
【问题讨论】:
你可以这样做:
select tbl.subject
,count(*) as record_count
-- add other aggregate functions as required
from thetable tbl
join (values
('somesubject' ,'2005-04-03')
,('othersubject','2008-07-06')
,('yetanother' ,'2013-12-11')
-- add as many subjects as required
) cutoff(subject,min_dt) on cutoff.subject = tbl.subject and cutoff.min_dt::date <= tbl.date_column
group by tbl.subject
【讨论】: