Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Merge rows based on common column values

Don07Jun 11 2020 — edited Jun 15 2020

Hello There,

I would like to know how can I merge rows based on common column values. Below are the details:

(A): Sample Query

SELECT 1925696 Trx_ID, 101 Line_Number, 'LINE' Trx_Type, NULL Line_Ref, 10  Amount, NULL Tax_Rate FROM dual UNION ALL

SELECT 1925696 Trx_ID, 102 Line_Number, 'LINE' Trx_Type, NULL Line_Ref, 20  Amount, NULL Tax_Rate FROM dual UNION ALL

SELECT 1925696 Trx_ID, 103 Line_Number, 'LINE' Trx_Type, NULL Line_Ref, 30  Amount, NULL Tax_Rate FROM dual UNION ALL

SELECT 1925696 Trx_ID, 104 Line_Number, 'TAX'  Trx_Type, 101     Line_Ref, 1.9 Amount, 19      Tax_Rate FROM dual UNION ALL

SELECT 1925696 Trx_ID, 105 Line_Number, 'TAX'  Trx_Type, 102     Line_Ref, 1    Amount, 5        Tax_Rate FROM dual UNION ALL

SELECT 1925696 Trx_ID, 106 Line_Number, 'TAX'  Trx_Type, 103     Line_Ref, 3    Amount, 10      Tax_Rate FROM dual

(B): Output from above sample query:

TRX_ID

LINE_NUMBER

TRX_TYPE

LINE_REF

AMOUNT

TAX_RATE

1925696

101

LINE

10

1925696

102

LINE

20

1925696

103

LINE

30

1925696

104

TAX

101

1.9

19

1925696

105

TAX

102

1

5

1925696

106

TAX

103

3

10

Common Column Values:

1. Column TRX_ID is the same for all rows

2. Column LINE_NUMBER  whose TRX_TYPE's (LINE)      =   LINE_REF  whose TRX_TYPE's (TAX)

(C): Expected Output:

TRX_ID

TRX_TYPE

LINE_NUMBER

LINE_AMOUNT

TAXABLE_AMOUNT

TAX_RATE

TAX_AMOUNT

1925696

LINE

101

1010

19

1.9

1925696

LINE

102

20

20

5

1

1925696

LINE

103

30

30

10

3

Could you please advice on the SQL query?

Thank you and look forward to hearing from you.

Regards,

Don

Comments
Post Details
Added on Jun 11 2020
7 comments
2,116 views