Search code examples
sqlpostgresqlapache-superset

Select Minimum Date within WHERE Clause


From a PostgreSQL perspective: I have a dataset which consists of indicator values, timestamps, and a flag to denote whether the indicator values pertain to the baseline or follow-up period.

I want to select all rows from the baseline period AND the first row from the follow-up period. I'm not able to use MIN(date) in a WHERE clause, and it's not that the follow-up period will always start on the same date in all cases.

Is there a way that I can do something like WHERE type = 'baseline' OR (type = 'fol' AND date <= MIN(date))?

From a Superset perspective: I'm using an Annotation Layer to highlight the portion of the indicator line that represents the baseline period. I then highlight the follow-up period with a different color. I need to make sure the highlighting is continuous (right now the line segment between baseline last date and the follow-up last date is not highlighted). So, I need to apply a filter to the chart underlying the baseline highlight Annotation Layer that returns data for the baseline period AND the first follow-up date.


Solution

  • There're several ways of doing this as I see:

    1) Use UNION to have two separate queries

    select <Query> where type = 'baseline'
    UNION ALL
    select <Query> where type = 'fol' order by date limit 1
    

    2) Use the where clause you wrote, but calculate the date in subquery as in @Kushal Arya answer

    3) Use window-function ROW_NUMBER

    select
        <Query>,
        ROW_NUMBER() over (partition by type
                           order by date) as row_num
    where
      type = 'baseline' OR
      (type = 'fol' AND row_num = 1)
    

    I didn't run these queries