[英]DB2 Get latest modified and previous value from audit table
我有一個審計表,我正在嘗試使用審計時間戳信息獲取列(排名)的當前值和先前值。 我想獲取值更改時的時間戳。 例如:
對於id = 1
,排名最近在 2021 年 5 月13-05-2021 14:10
由userid = 2
from 3 to 5
更改為 5。 我在下面寫了查詢,它給出了當前和以前的修改值,但它給出了latest date
和用戶 ID( 17-05-2021 20:00
和2
),因為 row_number 是按時間戳排序的。
with v_rank as (
select * from (
select
id,
a.rank as current_rank,
b.rank as previous_rank,
a.log_timestamp,
a.log_username,
row_number() over(partition by a.id order by a.log_timestamp) as rnum
from
user a
inner join user b on a.id = b.id and a.log_timestamp > b.timestamp
where
a.rank != b.rank
order by a.log_timestamp, b.timestamp
) where rnum = 1
)
select * from v_rank
關於如何獲得正確timestamp(13-05-2021 14:10)
和userid(2)
的任何建議。 編輯:排名也可以是 null,在這種情況下我需要在查詢結果中得到空白。
預期 output:
你似乎想要lag()
過濾:
select u.*
from (select u.*,
lag(rank) over (partition by id order by log_timestamp) as prev_rank
from user u
) u
where rank <> prev_rank;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.