-1

In oracle Query either pure SQL or PLSQL, is that possible with the given source table to have the expected result? Thanks

Source Table:

Col_Old_ID  Col_New_ID
ID_1        ID_2
ID_2        ID_3
ID_A        ID_B
ID_B        ID_C
ID_3        ID_4
ID_4        ID_5
ID_C        ID_D

Expected Result:

ID             History
ID_1           ID_1,ID_2,ID_3,ID_4,ID_5
ID_2           ID_1,ID_2,ID_3,ID_4,ID_5
ID_3           ID_1,ID_2,ID_3,ID_4,ID_5
ID_4           ID_1,ID_2,ID_3,ID_4,ID_5
ID_5           ID_1,ID_2,ID_3,ID_4,ID_5
ID_A           ID_A,ID_B,ID_C,ID_D
ID_B           ID_A,ID_B,ID_C,ID_D
ID_C           ID_A,ID_B,ID_C,ID_D
ID_D           ID_A,ID_B,ID_C,ID_D
2
  • I still finding way to format the question, sorry about that i now in this forum. @a_horse_with_no_name sorry is 11g Commented Apr 19, 2018 at 9:13
  • @a_horse_with_no_name, hi thanks for your helped, but i would like say i cant find the answer match with my case in other post, because my expected History is all same value with ID_1 until ID_4. But most of the given result is different. Commented Apr 19, 2018 at 9:21

1 Answer 1

1

SQL Fiddle

Oracle 11g R2 Schema Setup:

CREATE TABLE table_name ( Col_Old_ID, Col_New_ID ) AS
SELECT 'ID_1',        'ID_2' FROM DUAL UNION ALL
SELECT 'ID_2',        'ID_3' FROM DUAL UNION ALL
SELECT 'ID_A',        'ID_B' FROM DUAL UNION ALL
SELECT 'ID_B',        'ID_C' FROM DUAL UNION ALL
SELECT 'ID_3',        'ID_4' FROM DUAL UNION ALL
SELECT 'ID_4',        'ID_5' FROM DUAL UNION ALL
SELECT 'ID_C',        'ID_D' FROM DUAL;

Query 1: You can use a hierarchical query to get the history from the current row:

SELECT CONNECT_BY_ROOT( Col_Old_ID ) AS Id,
       CONNECT_BY_ROOT( Col_Old_ID )
         || SYS_CONNECT_BY_PATH( Col_New_ID, ',' ) AS History
FROM   table_name
WHERE  CONNECT_BY_ISLEAF = 1
CONNECT BY PRIOR Col_New_ID = Col_Old_ID

Results:

|   ID |                  HISTORY |
|------|--------------------------|
| ID_1 | ID_1,ID_2,ID_3,ID_4,ID_5 |
| ID_2 |      ID_2,ID_3,ID_4,ID_5 |
| ID_3 |           ID_3,ID_4,ID_5 |
| ID_4 |                ID_4,ID_5 |
| ID_A |      ID_A,ID_B,ID_C,ID_D |
| ID_B |           ID_B,ID_C,ID_D |
| ID_C |                ID_C,ID_D |

Query 2: If you want the full history then it gets complicated as you need the hierarchy before and after the item.

SELECT id,
       HISTORY
FROM   (
  SELECT Col_Old_ID,
         Col_new_id,
         MAX(
           SUBSTR( SYS_CONNECT_BY_PATH( Col_Old_ID, ',' ), 2 )
             || ',' || Col_New_ID
         ) OVER (
           PARTITION BY CONNECT_BY_ROOT( Col_Old_ID )
           ORDER BY     CONNECT_BY_ISLEAF DESC
         ) As history,
         CONNECT_BY_ISLEAF AS leaf
  FROM   table_name
  START WITH Col_Old_ID NOT IN ( SELECT Col_New_Id FROM table_name )
  CONNECT BY PRIOR Col_New_ID = Col_Old_ID
)
UNPIVOT ( id FOR col_type IN ( Col_Old_Id, Col_New_Id ) )
WHERE col_type = 'COL_OLD_ID'
OR    leaf = 1
ORDER BY id

Results:

|   ID |                  HISTORY |
|------|--------------------------|
| ID_1 | ID_1,ID_2,ID_3,ID_4,ID_5 |
| ID_2 | ID_1,ID_2,ID_3,ID_4,ID_5 |
| ID_3 | ID_1,ID_2,ID_3,ID_4,ID_5 |
| ID_4 | ID_1,ID_2,ID_3,ID_4,ID_5 |
| ID_5 | ID_1,ID_2,ID_3,ID_4,ID_5 |
| ID_A |      ID_A,ID_B,ID_C,ID_D |
| ID_B |      ID_A,ID_B,ID_C,ID_D |
| ID_C |      ID_A,ID_B,ID_C,ID_D |
| ID_D |      ID_A,ID_B,ID_C,ID_D |
Sign up to request clarification or add additional context in comments.

8 Comments

Hi, thanks your respond, but i the result that i want is the history have same result for ID_1 until ID_4, but your given result is not what i want.
@LeeJack Updated
Thanks!!! this the result i expect for.....i search a lot forum but not able to find this. finally i get it here...thanks!!!
Hi, i have try out in my query, is have around 300,000 record and give me about 614,944,004 cost. is that possible to tune this?
@LeeJack I've tuned it a lot but it probably can be tuned more - have a go and post a new answer if you find something more efficient.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.