请问这个SQL能实现吗? 又该如何写? CREATE TABLE TEST
( DT DATE,
NUM VARCHAR2(20 BYTE),
FLAG VARCHAR2(20 BYTE)
)
insert into test values(to_date('2013-10-30 10:00:00','yyyy-mm-dd hh24:mi:ss'),'1111111','A')
insert into test values(to_date('2013-10-30 11:00:00','yyyy-mm-dd hh24:mi:ss'),'1111111','A')
insert into test values(to_date('2013-10-30 11:10:00','yyyy-mm-dd hh24:mi:ss'),'2222222','A')
insert into test values(to_date('2013-10-30 12:00:00','yyyy-mm-dd hh24:mi:ss'),'1111111','A')
请问如何得到发生变化时记录原NUM,我想要的结果是两条记录
DT NUM FLAG OLD NUM
2013-10-30 11:10:00 2222222 A 1111111
2013-10-30 12:00:00 1111111 A 2222222
分享到:
------解决方案--------------------
嵌套一个子查询就可以了
select * from (
Select T.*, Lag(Num) Over (Partition By Flag Order By Dt Asc) As Last_Num
from test t)
where where num!=Last_Num