When I retrieve data using Select date,type,value from table1
It show the values like below.
But I want to customize it like below
Can anyone give me idea to do this?
CodePudding user response:
First we mark every time there's a change in Value
using lag
and ordering by Date
, and then we count
to create distinct groups.
select min(date) as "From Date"
,max(date) as "To Date"
,type
,value
from (
select *
,count(chng) over(order by date) as grp
from (
select *
,case when value <> lag(value) over(order by date) then 1 end as chng
from t
) t
) t
group by grp, type, value
order by 1
From Date | To Date | type | value |
---|---|---|---|
2022-09-21 | 2022-09-24 | S | 10 |
2022-09-25 | 2022-09-27 | S | 12 |
2022-09-28 | 2022-09-30 | S | 10 |