Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find 2nd maximum start_date for a group of set_id in big query

I have a table with data like below

enter image description here

I want the 2nd highest start date for each set where the status is C. e.g the answer should be

1 2015-05-01
2 2015-05-01
3 2015-06-01

Is there a simpler way to do this in bigquery?

like image 522
KRS Avatar asked Oct 30 '25 17:10

KRS


1 Answers

First filter only records which have status = 'C', then use window functions to partition data by set_id and order by start_date inside each set. Then take 2nd value.

SELECT 
  set_id, 
  NTH_VALUE(start_date, 2) OVER(PARTITION BY set_id ORDER BY start_date DESC)
FROM table WHERE status = 'C'
like image 141
Mosha Pasumansky Avatar answered Nov 01 '25 07:11

Mosha Pasumansky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!