Hi I’m trying to select the minimum and NOT the maximun date in a table. Instead of the maximun date I want to select the date of the fourth event. Any advice?
User | Event | Date |
---|---|---|
xyz | 1 | 2021-06-06 |
xyz | 2 | 2021-06-26 |
xyz | 3 | 2021-07-30 |
xyz | 4 | 2022-01-10 |
xyz | 5 | 2022-02-14 |
xyz | 6 | 2022-02-21 |
I was trying something like this:
select user, min(date), max(date) from table
where event <= 4
group by user
Thanks
2
Answers
You can use a sub-query for the part not affected by the WHERE.
One alternative to your current solution uses aggregation:
For a more general solution, we can use
RANK()
along with a pivot query:Depending on the ties behavior you want, using
DENSE_RANK()
orROW_NUMBER()
instead ofRANK()
might make sense.