oracle 使用累积值更新列-创建查询的困难

ef1yzkbh  于 2023-06-22  发布在  Oracle
关注(0)|答案(1)|浏览(138)

因此,我必须为某些组中的所有记录生成累积值(SUST-Y 01 - 06)。我做到了-输出屏幕和下面的查询。
查询:

SELECT
  sust_from, round(percentage_compared_to_sust,2) as percentage_compared_to_sust,
  round(SUM(percentage_compared_to_sust) OVER (PARTITION BY sust_from
  order by sust_from, percentage_compared_to_sust desc),2)
  AS cumulative
FROM table_01;

输出:Query output
当我必须更新表并使用这些查询值(作为累积值)设置列ACCUMULATE_PERCENTAGE时,困难就开始了。
我试着写了几个UPDATE语句,但每次都失败了。请,有人能给我提供一个很好的例子,这个更新应该是什么样子?

8fsztsew

8fsztsew1#

使用MERGE语句并在ROWID伪列上关联:

MERGE INTO table_01 dst
USING (
  SELECT ROWID AS rid,
         round(
           SUM(percentage_compared_to_sust) OVER (
             PARTITION BY sust_from
             order by sust_from, percentage_compared_to_sust desc
           ),
           2
         ) AS cumulative
  FROM   table_01
) src
ON (src.rid = dst.ROWID)
WHEN MATCHED THEN
  UPDATE
  SET accumulate_percentage = src.cumulative;

相关问题