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 9200333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:44:37+00:00 2026-06-17T22:44:37+00:00

I need to find the remaining amount for each credit no. The expected result

  • 0

I need to find the remaining amount for each credit no. The expected result is this:

CREDIT_NO   CREDIT_TYPE CREDIT_AMOUNT   TOTAL_A REMAINING_AMT   FINAL_TOTAL_PER_BILL
A1          W           100             1000     900            600
A1          X           100             1000     800            600
A1          Y           100             1000     700            600
A1          Z           100             1000     600            600
B1          X           100             2000     1900           1700
B1          Y           100             2000     1800           1700
B1          Z           100             2000     1700           1700

Here’s the query I have done so far (please pardon the noob):

WITH TEMP AS
  (SELECT 1 ID,
    'A1' CREDIT_NO,
    'X' CREDIT_TYPE,
    100 CREDIT_AMOUNT,
    1000 TOTAL_A
  FROM DUAL
  UNION ALL
  SELECT 2, 'A1' , 'Y', 100, 1000 FROM DUAL
  UNION ALL
  SELECT 4, 'A1' , 'Z', 100, 1000 FROM DUAL
  UNION ALL
  SELECT 3, 'B1', 'X', 100, 2000 FROM DUAL
  UNION ALL
  SELECT 5, 'B1', 'Y', 100, 2000 FROM DUAL
  UNION ALL
  SELECT 6, 'B1', 'Z', 100, 2000 FROM DUAL
  UNION ALL
  SELECT 7, 'A1', 'W', 100, 1000 FROM DUAL
  )

SELECT 
  TEMP1.CREDIT_NO ,
  TEMP1.CREDIT_TYPE,
  TEMP1.CREDIT_AMOUNT ,
  TEMP1.TOTAL_A ,
  CASE
    WHEN TEMP1.CREDIT_NO  = (LAG (TEMP1.CREDIT_NO,1) OVER (ORDER BY TEMP1.CREDIT_NO) ) -- set remaining CREDIT_AMOUNT
    OR (LAG (TEMP1.CREDIT_NO,1) OVER (ORDER BY TEMP1.CREDIT_NO) ) IS NULL
    THEN TEMP1.TOTAL_A - (SUM(TEMP1.CREDIT_AMOUNT) OVER ( ORDER BY TEMP1.CREDIT_NO ROWS BETWEEN
      UNBOUNDED PRECEDING
    AND CURRENT ROW ) )
    WHEN TEMP1.CREDIT_NO <>   -- new bill, new total CREDIT_AMOUNT
      (LAG (TEMP1.CREDIT_NO,1) OVER (ORDER BY TEMP1.CREDIT_NO) )
    THEN TEMP1.TOTAL_A - TEMP1.CREDIT_AMOUNT
  END AS REMAINING_AMT 
  ,TEMP1.TOTAL_A - (SUM(TEMP1.CREDIT_AMOUNT) OVER (PARTITION BY CREDIT_NO)) AS FINAL_TOTAL_PER_BILL
FROM TEMP TEMP1
ORDER BY CREDIT_NO, CREDIT_TYPE

My problem is I don’t know how to compute for the remaining amount for the 2nd credit no. The result of the above query is:

CREDIT_NO   CREDIT_TYPE CREDIT_AMOUNT   TOTAL_A REMAINING_AMT   FINAL_TOTAL_PER_BILL
A1          W           100             1000     900            600
A1          X           100             1000     800            600
A1          Y           100             1000     700            600
A1          Z           100             1000     600            600
B1          X           100             2000     1900           1700
B1          Y           100             2000     1400           1700
B1          Z           100             2000     1300           1700

Is it possible to get a running remaining amount without using a stored procedure? I tried basing it on the rownum but it is not sequential.

Even though I have found similar questions to this (Link 1, Link 2, Link 3)
(I’m still going over the third link though), I hope you guys can help me.

  • 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-17T22:44:38+00:00Added an answer on June 17, 2026 at 10:44 pm

    use a running subtotal, and be careful at partition clause as:

    select credit_no, credit_type, 
       total_a - sum(credit_amount) over (partition by credit_no order by id) as remaining_credit,
       total_a,
       total_a - sum(credit_amount) over (partition by credit_no) as FINAL_TOTAL_PER_BILL
    from temp 
    

    see sqlfiddle

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

Sidebar

Related Questions

Need to find the timestamp for the first minute of the first day of
I need to find a flexible solution for access control in PHP. In the
i need to find out automationid for key board values? how to send keystrokes
I need to find all the occurences of a file Test.TXT in c:\ or
I need to find the coordinates of windows open even when they are not
I need to find a silence in a mp3 file. Simple as that. For
I need to find out the value passed into an indexer. My code (c#)
I need to find a way to get actual page size in Google Chrome.
I need to find the longest sequence in a string with the caveat that
I need to find the mode EXE/DLL was build looking at its headers. (Using

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.