Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 883337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:34:20+00:00 2026-05-15T12:34:20+00:00

I’m trying to fix some errors on a big database of stock exchange data.

  • 0

I’m trying to fix some errors on a big database of stock exchange data. One column (quantity) has the traded volume on each tick, and other column stores the cumulative volume (i.e., the sum of the previous ticks of the day). This second column is wrong in some cases (not a lot, so we can safely assume that no to adjacent ticks are wrong). So theoretically the fix is easy: just search for a tick where the cumulative volume decreases (this suffices), and then pick the cumulative volume from the last tick and sum the quantity
of the current tick. The thing is that i’ve been trying to get to work a query that does this in oracle, but i’m struggling due to my lack of expertise in sql. This is what i’ve gotten so far:

update
( 
    select m.cumulative_volume, q.cum_volume_ant, q.quantity from 
    market_data_intraday_trades m
    join
    (
          select * from
          (select
            product_key,
            sequence_number,
            lead(product_key) over (order by product_key, sequence_number) as product_key_ant,
            to_char(trade_date_time, 'yyyymmdd') as fecha,
            to_char(lag(trade_date_time) over (order by product_key, sequence_number), 'yyyymmdd') as fecha_ant,
            cumulative_volume,
            lead(cumulative_volume) over (order by product_key, sequence_number) as cum_volume_ant,
            cumulative_volume - lead(cumulative_volume) over (order by product_key, sequence_number) as dif 
          from market_data_intraday_trades)
          where product_key = product_key_ant
          and fecha = fecha_ant
          and dif < 0 
          and rownum < 10
    ) q
    on m.sequence_number = q.sequence_number
)
set m.cumulative_volume = q.cum_volume_ant + q.quantity

The current problem being that i can’t seem to be able to use quantities from the inner query in the outside calculations.

Perhaps all of this would be clearer and/or easier with temporal tables or pl/sql or cursors, but due to corporate policies, i have no priviledges to do that, just selects and updates.

I would be very grateful if you could point me in some direction to solve this.

Thanks in advance!

PS. Fecha is date in spanish, just in case 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:34:21+00:00Added an answer on May 15, 2026 at 12:34 pm

    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 easiest solution is just to update all 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> 
    

    However, if you have a lot of data and you really don’t want all those unnecessary updates then use the same query again to restricted the hits:

    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>
    

    This solution is tidier then the other solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.