Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally selecting rows based on order

I have a table with columns score and timestamp. I need to select each row that score is a new high, when sorted by timestamp (bold rows):

score timestamp
6 1
3 2
3 3
5 4
7 5
10 6
8 7
9 8
10 9
11 10
like image 338
vpvp Avatar asked Aug 15 '26 08:08

vpvp


1 Answers

select   records
        ,min(Timestamp) as Timestamp
from    (
         select max(Score) over(order by timestamp) as records, Timestamp
         from t
         ) t
group by records  
records Timestamp
6 1
7 5
10 6
11 10

Fiddle

like image 162
DannySlor Avatar answered Aug 16 '26 23:08

DannySlor



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!