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

  • SEARCH
  • Home
  • 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 8426481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:32:38+00:00 2026-06-10T04:32:38+00:00

I have the following datasets (just a sample): Table1 : ID MAX AMT SORTED

  • 0

I have the following datasets (just a sample):

Table1:

ID    MAX    AMT  SORTED
1     20     0    1
1     30     0    2
1     40     0    3
1     50     0    4
2     0      0    1
2     30     0    2
2     40     0    3
2     40     0    4
...

Table2:

ID    AMT
1     75
2     70
...

I must update Table1.AMT from Table2.AMT using this rules:

  1. Table1 and Table2 are joined on ID
  2. Table1.AMT can’t hold larger value than MAX
  3. if Table2.AMT >= Table1.MAX then Table1.AMT = Table1.MAX… then on the next row update Table1.AMT with Table2.AMT - previous record AMT still using the above rules.

So the expected output would be

ID    MAX    AMT  SORTED
1     20     20   1
1     30     30   2
1     40     25   3
1     50     0    4
2     0      0    1
2     30     30   2
2     40     40   3
2     40     0    4
...

How can one achieve that?

I thought of creating a temp table with an aggregated SUM() of Table1.MAX, and using that as a reference to update Table1.AMT (if SUM(MAX) < Table2.AMT then Table1.AMT = Table1.MAX else Table1.AMT = previous records SUM(MAX)).

But can it be done without a temp table? (Sadly I can’t create functions and procedures in my work env.)

  • 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-06-10T04:32:40+00:00Added an answer on June 10, 2026 at 4:32 am

    More efficient solution can be made using specifics or Oracle PL/SQL.

    Here is a generic solution:

    select t1.ID, min(t1.MAX) as MAX, least(min(t1.MAX),coalesce(min(t2.AMT),0)-coalesce(least(sum(t1p.MAX-t1p.AMT), min(t2.AMT)),0)+min(t1.AMT)) as AMT, t1.SORTED
    from Table1 t1
    left join Table2 t2 on t2.ID = t1.ID
    left join Table1 t1p on t1p.ID = t1.ID and t1p.SORTED < t1.SORTED
    group by t1.ID, t1.SORTED 
    order by t1.ID, t1.SORTED 
    

    explanation of calculating AMT:

    AMT is smallest of “MAX for the row” and “How much is possible”

    least(min(t1.MAX),"How much is possible")
    

    “How much is possible”: max available – how much was given for previous rows + how much we already have

    coalesce(min(t2.AMT),0) - "how much was given for previous rows" + min(t1.AMT)
    

    “how much was given for previous rows”: smalles of how much required to fill and how much possible

    coalesce(least(sum(t1p.MAX-t1p.AMT), min(t2.AMT)),0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following sample dataset (below and/or as CSVs here: http://goo.gl/wK57T ) which
This is driving me crazy. I have a library I source from multiple scripts,
I have a hourly dataset from 1996 - 2010 in the following format: date
I am using an ASP.NET/C# DataList . <asp:DataList ID=EquipmentList RepeatColumns=5..... I have the following
I have following dataset: name1 <- c(P1, P2, IndA, IndB, IndC, IndD, IndE, IndF,
I have following dataset Id Title Group 1 title1 A 2 title2 A 3
I have big dataset (but the following is small one for example). I can
I have a dataset with ~20M rows and I'm observing the following behavior. The
I have following plist: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE plist PUBLIC -//Apple//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd>
I have following source. In insertMessage(..), it calls selectMessage to check whether duplicate record

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.