简体   繁体   中英

A different filter for each group by member

For each subject I would like to filter their records after a specific date. However, the date I want to use as a filter is different for each subject. Is there a way to group by subjects then for each group use a different having clause?

You can do something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM