简体   繁体   中英

Create SQL query for the given dataset

I have below dataset available.

在此处输入图像描述

I want to get below result.

在此处输入图像描述

I am trying to get the results using windowing functions, but am not able to find a way to do it. Can someone please help.!

Use row_number() to generate a sequence and subtract from value . That is a constant for the groups you want:

select element, min(value), max(value)
from (select t.*,
             row_number() over (partition by element order by value) as seqnum
      from t
     ) t
group by element, (value - seqnum)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM