也许是这样的?
样本数据:
SQL> with
2 test (id, code, sinfo, tdate) as
3 (select 1, 'X', 'Y', date '2007-01-01' from dual union all
4 select 1, 'X', 'Y', date '2007-04-06' from dual union all
5 select 1, 'X', 'Y', date '2007-04-09' from dual
6 ),
查询从这里开始:
7 temp as
8 (select id, code, sinfo,
9 tdate,
10 lead(tdate) over (partition by id order by tdate) next_tdate
11 from test
12 )
13 select id, code, sinfo,
14 tdate,
15 case when to_char(tdate, 'yyyymm') = to_char(next_tdate, 'yyyymm') then next_tdate
16 else tdate
17 end tenddate
18 from temp
19 where next_tdate is not null;
ID CODE SINFO TDATE TENDDATE
---------- ---- ----- ---------- ----------
1 X Y 01.01.2007 01.01.2007
1 X Y 06.04.2007 09.04.2007
SQL>