[英]Oracle get old and new value from audit table
Say I have an audit table structure kind of like this (simplified). 说我有一个类似这样的审核表结构(简化)。
OID,VALUE,CHANGED_DATE
and what I would like to do is write a query that, for each VALUE change, gives me the following output. 我想做的是编写一个查询,该查询对每个VALUE的更改都提供以下输出。
OLD_VALUE,NEW_VALUE,CHANGED_DATE
How would I go about doing that? 我将如何去做? So, for example, if I had 例如,如果我有
OID|VALUE|CHANGED_DATE
----------------------
1 |ABC |15-JAN-18
2 |DEF |22-JUN-18
3 |XYZ |16-JUL-18
I would like to see output like: 我希望看到如下输出:
OLD_VALUE|NEW_VALUE|CHANGED_DATE
--------------------------------
ABC |DEF |22-JUN-18
DEF |XYZ |16-JUL-18
Use lag()
: 使用lag()
:
select oid, lag(oid) over (order by changed_date) as prev_oid, changed_date
from t;
This code includes all three rows. 此代码包括所有三行。 If you want to eliminate the first: 如果要消除第一个:
select *
from (select oid, lag(oid) over (order by changed_date) as prev_oid, changed_date
from t
) t
where prev_oid is not null;
You want lead()
: 您想要lead()
:
select t.*
from (select oid, lead(value) over (order by changed_date) as next_value, changed_date
from table
) t
where next_value is not null;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.