Skip to Main Content
  • Questions
  • how to merge two records and show the results based on that

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Marco.

Asked: April 08, 2017 - 5:17 am UTC

Last updated: April 08, 2017 - 5:53 am UTC

Version: oracle 11g r2

Viewed 1000+ times

You Asked

Need a bit guidance in how to merge two records and show the results based on that.
So here i want to show what actual Qty is transferred from one location to another and show result based on that.
Here we can show debit qty or credit qty in result set


create table t1 (id Varchar2(4), location_id number, location_name Varchar2(10),debit_qty number, credit_qty number, trans_id number, status varchar2(10)); 
 
 
insert into t1 values ('1', '1001',  'U.S.', 10 , 6,1001,'Trans'); 
insert into t1 values ('2', '1002',  'CANADA', 5 , 10,1001,'Trans');  
 
insert into t1 values ('3', '1003',  'AUS', 20 , 4,1002,'Trans'); 
insert into t1 values ('4', '1004',  'IND', 8 , 20,1002,'Trans'); 



Desire output:

TID LOCATION_FROM DEBIT_QTY LOCATION_TO
1 U.S. 10 CANADA
2 AUS 20 IND
3 CANADA 20 IND

and Connor said...

I'm not entirely sure I understand your question. If it is just a single from-to pairing then perhaps

SQL> create table t1 (id Varchar2(4), location_id number, location_name Varchar2(10),debit_qty number, credit_qty number, trans_id number, status varchar2(10));

Table created.

SQL>
SQL> insert into t1 values ('1', '1001',  'U.S.', 10 , 6,1001,'Trans');

1 row created.

SQL> insert into t1 values ('2', '1002',  'CANADA', 5 , 10,1001,'Trans');

1 row created.

SQL>
SQL> insert into t1 values ('3', '1003',  'AUS', 20 , 4,1002,'Trans');

1 row created.

SQL> insert into t1 values ('4', '1004',  'IND', 8 , 20,1002,'Trans');

1 row created.

SQL>
SQL> select *
  2  from t1 t_from, t1 t_to
  3  where t_from.trans_id = t_to.trans_id
  4  and  t_from.id < t_to.id;

ID   LOCATION_ID LOCATION_N  DEBIT_QTY CREDIT_QTY   TRANS_ID STATUS     ID   LOCATION_ID LOCATION_N  DEBIT_QTY CREDIT_QTY   TRANS_ID STATUS
---- ----------- ---------- ---------- ---------- ---------- ---------- ---- ----------- ---------- ---------- ---------- ---------- ----------
1           1001 U.S.               10          6       1001 Trans      2           1002 CANADA              5         10       1001 Trans
3           1003 AUS                20          4       1002 Trans      4           1004 IND                 8         20       1002 Trans

2 rows selected.


If it is a hierarchy of pairings, you'd need to tell us what the linkage is between each row, for example:

https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:489772591421

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Great Response

Marco Foxx, April 08, 2017 - 6:07 am UTC

Hi Connor thanks for a quick reply.
I am sharing the script here i removed the ID which earlier i was using.
As you asked what is the linkage between two output there is trans_id which is common for two records.
You can track based on that every two records will have the same trans_id.
SQL> create table t1 (location_id number, location_name Varchar2(10),debit_qty number, credit_qty number, trans_id number, status varchar2(10));

Table created.

SQL>
SQL> insert into t1 values ('1001', 'U.S.', 10 , 6,1001,'Trans');

1 row created.

SQL> insert into t1 values ('1002', 'CANADA', 5, 10,1001,'Trans');

1 row created.

SQL>
SQL> insert into t1 values ('1003', 'AUS', 20 ,4,1002,'Trans');

1 row created.

SQL> insert into t1 values ('1004', 'IND', 8 ,20,1002,'Trans');