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 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 have two tables, movies and categories , and I want to get an
Say I have two tables I want to join. Categories: id name ---------- 1
Hi I want to have two tables each have an INT id column which
I have two tables containing Tasks and Notes, and want to retrieve a list
I have a dataset that has two tables in it. I want to do
I have two tables: Table 1: ID, PersonCode, Name, Table 2: ID, Table1ID, Location,
I have two tables: a source table and a target table. The target table
I have two fields in a table. One contains values such as BTA, BEA,
I have two tables in my database: NEWS table with columns: id - the
I have two functions to add and remove table rows that contain a form

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.