Home > Mobile >  A different filter for each group by member
A different filter for each group by member

Time:10-15

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?

CodePudding user response:

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,max_dt) on cutoff.subject = tbl.subject and cutoff.max_dt::date >= tbl.date_column
group by tbl.subject
  • Related