2012-04-13 3 views
1

Oracle SQL에 대한 도움이 필요합니다. 표의 데이터는 다음과 같습니다. ' 주어진 데이터에 대해 COB 날짜 범위와 값을 기반으로 밴드를 만들어야합니다.SQL 쿼리를 작성할 입력이 필요합니다.

COB   VALUE 
----------------- 
4/5/2012 20  
4/6/2012 20  
4/7/2012 22  
4/8/2012 21  
4/9/2012 20  
4/10/2012 21  
4/11/2012 21  
4/12/2012 22  
4/13/2012 20  

The output I am expecting is as given below:    

START_DT END_DT   VALUE 
---------------------------------- 
4/5/2012 4/7/2012 20 
4/7/2012 4/8/2012 22 
4/8/2012 4/9/2012 21 
4/9/2012 4/10/2012 20 
4/10/2012 4/12/2012 21 
4/12/2012 4/13/2012 22 
4/13/2012 null  20 

누군가 도와 줄 수 있습니까?

답변

7

lag and lead 친구가이 경우에

select cob start_dt 
,  lead(cob) over (order by cob) end_dt 
,  value 
from 
(
    select cob 
    ,  value 
    ,  lag(value) over (order by cob) prev_value 
    from tmp 
) 
where prev_value is null 
or value <> prev_value 

편집 : 고정 쿼리 :

편집하여 Rob van Wijk : 쿼리가 작동한다는 증거가 있습니다.

SQL> create table mytable(cob,value) 
    2 as 
    3 select date '2012-04-05', 20 from dual union all 
    4 select date '2012-04-06', 20 from dual union all 
    5 select date '2012-04-07', 22 from dual union all 
    6 select date '2012-04-08', 21 from dual union all 
    7 select date '2012-04-09', 20 from dual union all 
    8 select date '2012-04-10', 21 from dual union all 
    9 select date '2012-04-11', 21 from dual union all 
10 select date '2012-04-12', 22 from dual union all 
11 select date '2012-04-13', 20 from dual 
12/

Table created. 

SQL> select cob start_dt 
    2 ,  lead(cob) over (order by cob) end_dt 
    3 ,  value 
    4 from 
    5 ( select cob 
    6  ,  value 
    7  ,  lag(value) over (order by cob) prev_value 
    8  from mytable 
    9 ) 
10 where prev_value is null 
11 or value <> prev_value 
12/

START_DT   END_DT     VALUE 
------------------- ------------------- ---------- 
05-04-2012 00:00:00 07-04-2012 00:00:00   20 
07-04-2012 00:00:00 08-04-2012 00:00:00   22 
08-04-2012 00:00:00 09-04-2012 00:00:00   21 
09-04-2012 00:00:00 10-04-2012 00:00:00   20 
10-04-2012 00:00:00 12-04-2012 00:00:00   21 
12-04-2012 00:00:00 13-04-2012 00:00:00   22 
13-04-2012 00:00:00        20 

7 rows selected. 
+0

답장을 보내 주셔서 감사합니다. –

0

는 의사 코드에서는이 같은 것을 수행 할 수 있습니다 :

startDate = min(COB) from table 
actualDate = min(COB) from table 
lastDate = max(COB) from table 

lastValue = value from actualDate 


while actualDate <= lastDate 
    actualValue = value from actualDate 

    if actualValue <> lastValue 
    insert in temptable startdate, actualdate, actualValue 
    startdate = actualdate 

    actualdate = actualdate + 1 day 



insert in temptable startdate, null, actualValue 

select * from temptable 
관련 문제