如果我理解正確,只要它們對連續日期相同即可。
您可以使用窗口函數根據值生成組,並增加日期順序,然後查找所需的聚合。
with your_table(daytime ,value) as (
select to_date('13.02.2017','dd.mm.yyyy'),25000 from dual union all
select to_date('14.02.2017','dd.mm.yyyy'),20000 from dual union all
select to_date('15.01.2017','dd.mm.yyyy'),90000 from dual union all
select to_date('16.01.2017','dd.mm.yyyy'),90000 from dual union all
select to_date('17.01.2017','dd.mm.yyyy'),95800 from dual union all
select to_date('18.01.2017','dd.mm.yyyy'),95800 from dual union all
select to_date('19.01.2017','dd.mm.yyyy'),95800 from dual union all
select to_date('20.01.2017','dd.mm.yyyy'),95800 from dual union all
select to_date('21.01.2017','dd.mm.yyyy'),95800 from dual union all
select to_date('22.01.2017','dd.mm.yyyy'),95800 from dual union all
select to_date('23.01.2017','dd.mm.yyyy'),95800 from dual union all
select to_date('24.01.2017','dd.mm.yyyy'),90000 from dual union all
select to_date('25.01.2017','dd.mm.yyyy'),90000 from dual union all
select to_date('26.01.2017','dd.mm.yyyy'),90000 from dual
)
select
min(daytime) fromdate,
max(daytime) todate,
value
from (
select
t.*,
sum(x) over (order by daytime) grp
from (
select
t.*,
case when value = lag(value) over (order by daytime)
then 0 else 1 end x
from your_table t
) t
) t group by grp, value
order by fromdate;
產地:
FROMDATE TODATE VALUE
15-JAN-17 16-JAN-17 90000
17-JAN-17 23-JAN-17 95800
24-JAN-17 26-JAN-17 90000
13-FEB-17 13-FEB-17 25000
14-FEB-17 14-FEB-17 20000
賓果!有用!非常感謝你:) – akira
不確定你的意思是N.B. - 也許你的意思是,如果頂部有十行,所有值都是20000,但日期是連續四天,那麼一個差距,然後連續六天,這應該產生兩行而不是一行...如果這就是你的意思,然後** Tabibitosan **的小修改可以直接處理。要創建'grp',使用'daytime'本身而不是第一個'row_number()';這樣'grp'值最終會成爲日期而不是數字,但整個事情的工作方式完全相同。 – mathguy
好點,ta;我從來沒有想過要這樣做!星期一將有一個玩弄* {:-) – Boneist