Database Oracle使用n-2数据更新n-1列(如果可用),否则为“新建”
我们正在使用Oracle v12+ 在这种情况下,每当我们第一次为每个ID更新时,我们都需要将状态列更新为以前的值,因为它是功能ID,并且针对一个ID有许多行 请参见下面的示例i/o。数据按每个ID的时间戳排序 我们需要特定的联接来更新吗?我已经将数据复制到临时表中,但没有成功 表名称:表\账簿\状态 您可以按如下方式使用滞后和求和窗口功能:Database Oracle使用n-2数据更新n-1列(如果可用),否则为“新建”,database,oracle,select,oracle12c,insert-update,Database,Oracle,Select,Oracle12c,Insert Update,我们正在使用Oracle v12+ 在这种情况下,每当我们第一次为每个ID更新时,我们都需要将状态列更新为以前的值,因为它是功能ID,并且针对一个ID有许多行 请参见下面的示例i/o。数据按每个ID的时间戳排序 我们需要特定的联接来更新吗?我已经将数据复制到临时表中,但没有成功 表名称:表\账簿\状态 您可以按如下方式使用滞后和求和窗口功能: select id, word, case when sm = 1 and status = 'Renew' t
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)
lagstatus按id顺序按时间戳划分,因为lgst多次为null,但在内部查询中状态为值@特贾肖克!!!更新了答案。在CASE语句中添加了else。现在检查!太棒了!!!超级接近+感谢你的努力。你能帮我把它改成更新脚本吗@Tejashor可以是PL/SQL@Tejash
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)