user287023
user287023

Reputation: 93

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?

Upvotes: 0

Views: 186

Answers (1)

Bill Jetzer
Bill Jetzer

Reputation: 1115

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

Upvotes: 1

Related Questions