Oracle 使用 n-2 数据更新 n-1 列(如果可用)其他 'NEW'
Oracle Update n-1 Column With n-2 Data if available else 'NEW'
我们正在使用 Oracle v12+
我们有这样一种情况,我们需要将 status
列更新为以前的值 每当我们有第一个 RENEW
时每个 ID
(它是功能 ID,一个 ID 有很多行)。
请参阅下面的示例 i/o。数据按每个 ID 的时间戳排序。
我们需要更新特定的连接吗?为此,我已将数据复制到临时 table,但没有成功。
Table 姓名:table_book_status
Input
[id] [word] [status] [timestamp]
B000JMLBHU book RENEW
B000JMLBHU read RENEW
B000JMLBHU was MODIFY
B000JMLBHU story ADD
B000R93D4Y with RENEW
B000R93D4Y book RENEW
B000R93D4Y story RENEW
B000R93D4Y was MODIFY
B000R93D4Y have ADD
B001892DGG was ADD
B001892DWA was ADD
B001BXNQ2O was RENEW
B001BXNQ2O book RENEW
B001H55R8M was MODIFY
B001HQHCBQ was ADD
B001HQHCBQ story ADD
B001HQHCBQ bella ADD
B001HQHCBQ with ADD
B001HQHCBQ love ADD
B001HQHCBQ zsadist ADD
Output
[id] [word] [status] [timestamp]
B000JMLBHU book RENEW
B000JMLBHU read **MODIFY**
B000JMLBHU was MODIFY
B000JMLBHU story ADD
B000R93D4Y with RENEW
B000R93D4Y book RENEW
B000R93D4Y story **MODIFY**
B000R93D4Y was MODIFY
B000R93D4Y have ADD
B001892DGG was ADD
B001892DWA was ADD
B001BXNQ2O was RENEW
B001BXNQ2O book **ADD**
B001H55R8M was MODIFY
B001HQHCBQ was ADD
B001HQHCBQ story ADD
B001HQHCBQ bella ADD
B001HQHCBQ with ADD
B001HQHCBQ love ADD
B001HQHCBQ zsadist ADD
您可以使用lag
和sum
window功能如下:
select id, word,
case when sm = 1 and status = 'Renew'
then coalesce(lgst, 'Add')
else status
end as status,
timestamp
from
(select id, word, status,
sum(case when status = 'Renew' then 1 else 0 end) over (partition by id order by timestamp) as sm,
lag(status) over (partition by id order by timestamp) as lgst, timestamp
from your_table)
我们正在使用 Oracle v12+
我们有这样一种情况,我们需要将 status
列更新为以前的值 每当我们有第一个 RENEW
时每个 ID
(它是功能 ID,一个 ID 有很多行)。
请参阅下面的示例 i/o。数据按每个 ID 的时间戳排序。
我们需要更新特定的连接吗?为此,我已将数据复制到临时 table,但没有成功。
Table 姓名:table_book_status
Input
[id] [word] [status] [timestamp]
B000JMLBHU book RENEW
B000JMLBHU read RENEW
B000JMLBHU was MODIFY
B000JMLBHU story ADD
B000R93D4Y with RENEW
B000R93D4Y book RENEW
B000R93D4Y story RENEW
B000R93D4Y was MODIFY
B000R93D4Y have ADD
B001892DGG was ADD
B001892DWA was ADD
B001BXNQ2O was RENEW
B001BXNQ2O book RENEW
B001H55R8M was MODIFY
B001HQHCBQ was ADD
B001HQHCBQ story ADD
B001HQHCBQ bella ADD
B001HQHCBQ with ADD
B001HQHCBQ love ADD
B001HQHCBQ zsadist ADD
Output
[id] [word] [status] [timestamp]
B000JMLBHU book RENEW
B000JMLBHU read **MODIFY**
B000JMLBHU was MODIFY
B000JMLBHU story ADD
B000R93D4Y with RENEW
B000R93D4Y book RENEW
B000R93D4Y story **MODIFY**
B000R93D4Y was MODIFY
B000R93D4Y have ADD
B001892DGG was ADD
B001892DWA was ADD
B001BXNQ2O was RENEW
B001BXNQ2O book **ADD**
B001H55R8M was MODIFY
B001HQHCBQ was ADD
B001HQHCBQ story ADD
B001HQHCBQ bella ADD
B001HQHCBQ with ADD
B001HQHCBQ love ADD
B001HQHCBQ zsadist ADD
您可以使用lag
和sum
window功能如下:
select id, word,
case when sm = 1 and status = 'Renew'
then coalesce(lgst, 'Add')
else status
end as status,
timestamp
from
(select id, word, status,
sum(case when status = 'Renew' then 1 else 0 end) over (partition by id order by timestamp) as sm,
lag(status) over (partition by id order by timestamp) as lgst, timestamp
from your_table)