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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:40:41+00:00 2026-05-24T02:40:41+00:00

I have a two table. I want to get the below result out from

  • 0

I have a two table. I want to get the below result out from these two table. Any help appreciate.


EVENT table

event_id | gross_amount | transaction_id
1        | 10           | 1
2        | 12           | 5

TRANSACTION table

trx_id | debit | credit | link_trx_id
1      | 4     | 0      | null
2      | 0     | 2      | 1
3      | 0     | 1      | 2
4      | 3     | 0      | 3
5      | 0     | 5      | null
6      | 0     | 3      | 5

RESULT EXPECTED:

trx_id | debit | credit | current_gross | current_net
1      | 4     | 0      | 10            | 6
2      | 0     | 2      | 6             | 8
3      | 0     | 1      | 8             | 9
4      | 3     | 0      | 9             | 6
5      | 0     | 5      | 10            | 15
6      | 0     | 3      | 15            | 18

Explanation

As you can see transaction 1,2,3,4 falling into an one set while 4,6 falling into an another set. For the each transaction needed the it’s linked previous transactions current_net value as a it’s current_gross.

Basically getting current_gross is a recursive call. Here I can not use PL SQL function where I can write a quick recursive function to calculate current_gross. I need pure PL/SQL query for this task. ( Can use built in PL SQL functions)

  • 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-24T02:40:42+00:00Added an answer on May 24, 2026 at 2:40 am

    Using Allan’s query, I added the create table and inserts.
    The query had mismatched variables so I correct those as well (debit_cum/credit_cum did not match the cum-credit/cum_debit variables in the sub-query).

    create table event
    (event_id   number(9),
     gross_amount number(9),
     transaction_id number(9));
    
     insert into event values (1,10,1);
     insert into event values (2,12,5);
    
     create table transaction
     (trx_id number(9),
      debit  number(9),
      credit number(9),
      link_trx_id number(9)
      );
    
      insert into transaction values (1,4,0,null);
      insert into transaction values (2,0,2,1);
      insert into transaction values (3,0,1,2);
      insert into transaction values (4,3,0,3);
      insert into transaction values (5,0,5,null);
      insert into transaction values (6,0,3,5);
    
    
    
    SELECT trx_id,
           debit,
           credit,
           root_amt - debit_cum + credit_cum + debit - credit AS current_gross,
           root_amt - debit_cum + credit_cum AS current_net
    FROM   (SELECT trx_id,
                   debit,
                   credit,
                   SUM(credit) OVER (PARTITION BY event_id ORDER BY lvl) AS credit_cum,
                   SUM(debit) OVER (PARTITION BY event_id ORDER BY lvl) AS debit_cum,
                   root_amt,
                   event_id
            FROM   (SELECT     trx_id,
                               debit,
                               credit,
                               LEVEL AS lvl,
                               CONNECT_BY_ROOT (gross_amount) AS root_amt,
                               CONNECT_BY_ROOT (event_id) AS event_id
                    FROM       transaction t LEFT OUTER JOIN event e ON t.trx_id = e.transaction_id
                    CONNECT BY link_trx_id = PRIOR trx_id
                    START WITH link_trx_id IS NULL));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get data from two tables. I have patient first name and
I use the code below to get records from two tables but I want
I have two table emptable1(empid,status) emptable2(empid,week) i want to select all the empid whose
I have two table first is TABLE_SUBJECT and second is TABLE_CHAPTER.Now i want to
I have two tables Loan(Parent Table) and Receipt(Child table)what i want to do is
I want to place a table on my page. I have two tables in
I have a really simple table with two rows and two columns. I want
I have a SQL table with only two columns: nameid and name. I want
I have two mysql tables and i want to merge the results of these
I have two tables, I want to search TermID in Table-A through TermID in

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.