select id, newphone, oldphone
from (select id,
phone as newphone,
(select phone
from testa c
where c.date1 = (select max(date1)
from testa b
where b.date1 < a.date1
and c.id = b.id)
and c.id = a.id) as oldphone
from testa a)
where oldphone is not null
select id ,newphone, oldphone from
(select id ,phone as newphone,(select max(phone) from testa b where b.date1 < a.date1) as oldphone
from testa a)where oldphone is not null
with t as (
select 'A' as phone,1 as times from dual
union all
select 'B' as phone,2 as times from dual
union all
select 'C' as phone,3 as times from dual
)
select phone||'-'||phone1 from (
select phone,lead(phone) over(order by times) as phone1 from t)t1
where phone1 is not null
SELECT a.subs_id,
a.phone ,a.new_phone FROM ( SELECT a.subs_id,
a.phone ,
CASE WHEN a.time<b.time THEN b.phone END new_phone
FROM CMPDATA_TB_SUBS_IMEI_CHAG_DTL a, CMPDATA_TB_SUBS_IMEI_CHAG_DTL b
WHERE a.subs_id=b.subs_id )a WHERE a.new_imei>0;
这是我写的多了A-C的记录