Here is some test data. As you can see the CUMULATIVE_VOLUME for the fourth row is wrong.
SQL> select product_key
2 , trade_date_time
3 , quantity
4 , cumulative_volume
5 , sum (quantity) over (partition by product_key order by sequence_number) as running_total
6 from market_data_intraday_trades
7 order by sequence_number
8 /
PROD TRADE_DAT QUANTITY CUMULATIVE_VOLUME RUNNING_TOTAL
---- --------- ---------- ----------------- -------------
ORCL 23-JUN-10 100 100 100
ORCL 23-JUN-10 50 150 150
ORCL 25-JUN-10 100 250 250
ORCL 26-JUN-10 100 250 350
ORCL 26-JUN-10 50 400 400
ORCL 27-JUN-10 75 475 475
6 rows selected.
SQL>
最简单的解决方案就是更新。
全部的
the rows with the calculated running total:
SQL> update market_data_intraday_trades m
2 set m.cumulative_volume =
3 ( select inq.running_total
4 from (
5 select sum (quantity) over (partition by product_key
6 order by sequence_number) as running_total
7 , cumulative_volume
8 , rowid as row_id
9 from market_data_intraday_trades
10 ) inq
11 where m.rowid = inq.row_id
12 )
13 /
6 rows updated.
SQL> select product_key
2 , trade_date_time
3 , quantity
4 , cumulative_volume
5 , sum (quantity) over (partition by product_key
6 order by sequence_number) as running_total
7 , rowid as row_id
8 from market_data_intraday_trades
9 order by sequence_number
10 /
PROD TRADE_DAT QUANTITY CUMULATIVE_VOLUME RUNNING_TOTAL
---- --------- ---------- ----------------- -------------
ORCL 23-JUN-10 100 100 100
ORCL 23-JUN-10 50 150 150
ORCL 25-JUN-10 100 250 250
ORCL 26-JUN-10 100 350 350
ORCL 26-JUN-10 50 400 400
ORCL 27-JUN-10 75 475 475
6 rows selected.
SQL>
但是,如果您有大量数据,并且确实不需要所有这些不必要的更新,那么再次使用相同的查询来限制点击量:
SQL> update market_data_intraday_trades m
2 set m.cumulative_volume =
3 ( select inq.running_total
4 from (
5 select sum (quantity) over (partition by product_key
6 order by sequence_number) as running_total
7 , cumulative_volume
8 , rowid as row_id
9 from market_data_intraday_trades
10 ) inq
11 where m.rowid = inq.row_id
12 )
13 where m.rowid in
14 ( select inq.row_id
15 from (
16 select sum (quantity) over (partition by product_key
17 order by sequence_number) as running_total
18 , cumulative_volume
19 , rowid as row_id
20 from market_data_intraday_trades
21 ) inq
22 where m.cumulative_volume != running_total
23 )
24
SQL> /
1 row updated.
SQL> select product_key
2 , trade_date_time
3 , quantity
4 , cumulative_volume
5 , sum (quantity) over (partition by product_key
6 order by sequence_number) as running_total
7 from market_data_intraday_trades
8 order by sequence_number
9 /
PROD TRADE_DAT QUANTITY CUMULATIVE_VOLUME RUNNING_TOTAL
---- --------- ---------- ----------------- -------------
ORCL 23-JUN-10 100 100 100
ORCL 23-JUN-10 50 150 150
ORCL 25-JUN-10 100 250 250
ORCL 26-JUN-10 100 350 350
ORCL 26-JUN-10 50 400 400
ORCL 27-JUN-10 75 475 475
6 rows selected.
SQL>
I tried Nicolas's suggestion of using MERGE. If you are using 10g or higher, then this would work. You need a recent version of Oracle because 9i didn't support MERGE with an UPDATE but no INSERT (and 8i didn't support MERGE at all).
SQL> merge into market_data_intraday_trades m
2 using ( select running_total
3 , row_id
4 from
5 ( select sum (quantity) over (partition by product_key
6 order by sequence_number) as running_total
7 , cumulative_volume
8 , rowid as row_id
9 from market_data_intraday_trades
10 )
11 where cumulative_volume != running_total
12 ) inq
13 on ( m.rowid = inq.row_id )
14 when matched then
15 update set m.cumulative_volume = inq.running_total
16 /
1 row merged.
SQL>
这个解决方案比另一个方案更简洁。