Oracle Complex Hierarchy
up vote
0
down vote
favorite
In general, I have a Key (Contract) that changes to another key in a row.
The changed Key can be changed and for that we have another row.
The key go back as it was at the beginning.
I need a row for the first Key (where it all started) and the newest Key (that is skipping all the other keys that were in between).
This creates the table:
CREATE TABLE CONTRACT ("NAME" VARCHAR2(20 BYTE)
,"OLD_CONTRACT" VARCHAR2(20 BYTE)
,"NEW_CONTRACT" VARCHAR2(20 BYTE)
,"NEW_CONTRACT_DATE" NUMBER(10)) ;
COMMIT;
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('John','1','10',20180101);
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('Ronnie','10','6',20180107);
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('Kim','6','1',20180128);
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('Nathaly','3','2',20180419);
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('Gorge','2','8',20180713);
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('Allen','8','20',20180921);
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('Tom','4','11',20170103);
COMMIT;
This is how it looks
Name old_key new Key Updated date
---- ------- ------- ------------
John 1 10 20180101
Ronnie 10 6 20180107
Kim 6 1 20180128
Nathaly 3 2 20180419
Gorge 2 8 20180713
Allen 8 20 20180921
Tom 4 11 20170103
First 3 rows returns only ONE ROW.
Old Key New Key
------- -------
1 1
Why? Because
1 Becomes 10
10 Becomes 6
6 Becomes 1
Next 3 rows returns only ONE ROW.
Old Key New Key
------- -------
3 20
Why? Because
3 Becomes 2
2 Becomes 8
8 Becomes 20
Last row returns ONE ROW.
Old Key New Key
------- -------
4 11
Why? Because
4 Becomes 11
I need help writing Query for this scenario
sql oracle hierarchy
add a comment |
up vote
0
down vote
favorite
In general, I have a Key (Contract) that changes to another key in a row.
The changed Key can be changed and for that we have another row.
The key go back as it was at the beginning.
I need a row for the first Key (where it all started) and the newest Key (that is skipping all the other keys that were in between).
This creates the table:
CREATE TABLE CONTRACT ("NAME" VARCHAR2(20 BYTE)
,"OLD_CONTRACT" VARCHAR2(20 BYTE)
,"NEW_CONTRACT" VARCHAR2(20 BYTE)
,"NEW_CONTRACT_DATE" NUMBER(10)) ;
COMMIT;
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('John','1','10',20180101);
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('Ronnie','10','6',20180107);
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('Kim','6','1',20180128);
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('Nathaly','3','2',20180419);
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('Gorge','2','8',20180713);
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('Allen','8','20',20180921);
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('Tom','4','11',20170103);
COMMIT;
This is how it looks
Name old_key new Key Updated date
---- ------- ------- ------------
John 1 10 20180101
Ronnie 10 6 20180107
Kim 6 1 20180128
Nathaly 3 2 20180419
Gorge 2 8 20180713
Allen 8 20 20180921
Tom 4 11 20170103
First 3 rows returns only ONE ROW.
Old Key New Key
------- -------
1 1
Why? Because
1 Becomes 10
10 Becomes 6
6 Becomes 1
Next 3 rows returns only ONE ROW.
Old Key New Key
------- -------
3 20
Why? Because
3 Becomes 2
2 Becomes 8
8 Becomes 20
Last row returns ONE ROW.
Old Key New Key
------- -------
4 11
Why? Because
4 Becomes 11
I need help writing Query for this scenario
sql oracle hierarchy
Could you provide expected output? Only 2 columnsOld Key
andNew Key
?
– Pham X. Bach
Nov 21 at 9:12
So what are the rules for deciding what value ofold_key
to start with? The first example1 -> 1
seems particularly troubling.
– APC
Nov 21 at 11:05
"Could you provide expected output?" .... Yes. 2 Columns- Old key & New Key "So what are the rules for deciding what value..." There are many keys , there is no main Key
– Roni
Nov 22 at 9:33
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
In general, I have a Key (Contract) that changes to another key in a row.
The changed Key can be changed and for that we have another row.
The key go back as it was at the beginning.
I need a row for the first Key (where it all started) and the newest Key (that is skipping all the other keys that were in between).
This creates the table:
CREATE TABLE CONTRACT ("NAME" VARCHAR2(20 BYTE)
,"OLD_CONTRACT" VARCHAR2(20 BYTE)
,"NEW_CONTRACT" VARCHAR2(20 BYTE)
,"NEW_CONTRACT_DATE" NUMBER(10)) ;
COMMIT;
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('John','1','10',20180101);
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('Ronnie','10','6',20180107);
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('Kim','6','1',20180128);
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('Nathaly','3','2',20180419);
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('Gorge','2','8',20180713);
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('Allen','8','20',20180921);
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('Tom','4','11',20170103);
COMMIT;
This is how it looks
Name old_key new Key Updated date
---- ------- ------- ------------
John 1 10 20180101
Ronnie 10 6 20180107
Kim 6 1 20180128
Nathaly 3 2 20180419
Gorge 2 8 20180713
Allen 8 20 20180921
Tom 4 11 20170103
First 3 rows returns only ONE ROW.
Old Key New Key
------- -------
1 1
Why? Because
1 Becomes 10
10 Becomes 6
6 Becomes 1
Next 3 rows returns only ONE ROW.
Old Key New Key
------- -------
3 20
Why? Because
3 Becomes 2
2 Becomes 8
8 Becomes 20
Last row returns ONE ROW.
Old Key New Key
------- -------
4 11
Why? Because
4 Becomes 11
I need help writing Query for this scenario
sql oracle hierarchy
In general, I have a Key (Contract) that changes to another key in a row.
The changed Key can be changed and for that we have another row.
The key go back as it was at the beginning.
I need a row for the first Key (where it all started) and the newest Key (that is skipping all the other keys that were in between).
This creates the table:
CREATE TABLE CONTRACT ("NAME" VARCHAR2(20 BYTE)
,"OLD_CONTRACT" VARCHAR2(20 BYTE)
,"NEW_CONTRACT" VARCHAR2(20 BYTE)
,"NEW_CONTRACT_DATE" NUMBER(10)) ;
COMMIT;
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('John','1','10',20180101);
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('Ronnie','10','6',20180107);
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('Kim','6','1',20180128);
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('Nathaly','3','2',20180419);
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('Gorge','2','8',20180713);
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('Allen','8','20',20180921);
INSERT INTO CONTRACT (NAME,OLD_CONTRACT,NEW_CONTRACT,NEW_CONTRACT_DATE)
VALUES ('Tom','4','11',20170103);
COMMIT;
This is how it looks
Name old_key new Key Updated date
---- ------- ------- ------------
John 1 10 20180101
Ronnie 10 6 20180107
Kim 6 1 20180128
Nathaly 3 2 20180419
Gorge 2 8 20180713
Allen 8 20 20180921
Tom 4 11 20170103
First 3 rows returns only ONE ROW.
Old Key New Key
------- -------
1 1
Why? Because
1 Becomes 10
10 Becomes 6
6 Becomes 1
Next 3 rows returns only ONE ROW.
Old Key New Key
------- -------
3 20
Why? Because
3 Becomes 2
2 Becomes 8
8 Becomes 20
Last row returns ONE ROW.
Old Key New Key
------- -------
4 11
Why? Because
4 Becomes 11
I need help writing Query for this scenario
sql oracle hierarchy
sql oracle hierarchy
edited Nov 21 at 11:06
APC
116k15114227
116k15114227
asked Nov 21 at 8:42
Roni
42
42
Could you provide expected output? Only 2 columnsOld Key
andNew Key
?
– Pham X. Bach
Nov 21 at 9:12
So what are the rules for deciding what value ofold_key
to start with? The first example1 -> 1
seems particularly troubling.
– APC
Nov 21 at 11:05
"Could you provide expected output?" .... Yes. 2 Columns- Old key & New Key "So what are the rules for deciding what value..." There are many keys , there is no main Key
– Roni
Nov 22 at 9:33
add a comment |
Could you provide expected output? Only 2 columnsOld Key
andNew Key
?
– Pham X. Bach
Nov 21 at 9:12
So what are the rules for deciding what value ofold_key
to start with? The first example1 -> 1
seems particularly troubling.
– APC
Nov 21 at 11:05
"Could you provide expected output?" .... Yes. 2 Columns- Old key & New Key "So what are the rules for deciding what value..." There are many keys , there is no main Key
– Roni
Nov 22 at 9:33
Could you provide expected output? Only 2 columns
Old Key
and New Key
?– Pham X. Bach
Nov 21 at 9:12
Could you provide expected output? Only 2 columns
Old Key
and New Key
?– Pham X. Bach
Nov 21 at 9:12
So what are the rules for deciding what value of
old_key
to start with? The first example 1 -> 1
seems particularly troubling.– APC
Nov 21 at 11:05
So what are the rules for deciding what value of
old_key
to start with? The first example 1 -> 1
seems particularly troubling.– APC
Nov 21 at 11:05
"Could you provide expected output?" .... Yes. 2 Columns- Old key & New Key "So what are the rules for deciding what value..." There are many keys , there is no main Key
– Roni
Nov 22 at 9:33
"Could you provide expected output?" .... Yes. 2 Columns- Old key & New Key "So what are the rules for deciding what value..." There are many keys , there is no main Key
– Roni
Nov 22 at 9:33
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Here is a query using CONNECT BY that does the connection that you want and need.
From its results you have to select the first old_contract and the last new_contract.
SELECT NAME,OLD_CONTRACT,NEW_CONTRACT, LEVEL, rownum
FROM contract
START WITH OLD_CONTRACT = 3
CONNECT BY NOCYCLE PRIOR NEW_CONTRACT = OLD_CONTRACT
Results
NAME OLD_CONTRACT NEW_CONTRACT LEVEL ROWNUM
Nathaly 3 2 1 1
Gorge 2 8 2 2
Allen 8 20 3 3
Thank you! It canno't have "START WITH" because the are many keys at the same level. second , the key is varchar so there is no ordering. third , you didn't mention how to identify the "group" to take the first row and the last.
– Roni
Nov 22 at 9:39
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Here is a query using CONNECT BY that does the connection that you want and need.
From its results you have to select the first old_contract and the last new_contract.
SELECT NAME,OLD_CONTRACT,NEW_CONTRACT, LEVEL, rownum
FROM contract
START WITH OLD_CONTRACT = 3
CONNECT BY NOCYCLE PRIOR NEW_CONTRACT = OLD_CONTRACT
Results
NAME OLD_CONTRACT NEW_CONTRACT LEVEL ROWNUM
Nathaly 3 2 1 1
Gorge 2 8 2 2
Allen 8 20 3 3
Thank you! It canno't have "START WITH" because the are many keys at the same level. second , the key is varchar so there is no ordering. third , you didn't mention how to identify the "group" to take the first row and the last.
– Roni
Nov 22 at 9:39
add a comment |
up vote
0
down vote
Here is a query using CONNECT BY that does the connection that you want and need.
From its results you have to select the first old_contract and the last new_contract.
SELECT NAME,OLD_CONTRACT,NEW_CONTRACT, LEVEL, rownum
FROM contract
START WITH OLD_CONTRACT = 3
CONNECT BY NOCYCLE PRIOR NEW_CONTRACT = OLD_CONTRACT
Results
NAME OLD_CONTRACT NEW_CONTRACT LEVEL ROWNUM
Nathaly 3 2 1 1
Gorge 2 8 2 2
Allen 8 20 3 3
Thank you! It canno't have "START WITH" because the are many keys at the same level. second , the key is varchar so there is no ordering. third , you didn't mention how to identify the "group" to take the first row and the last.
– Roni
Nov 22 at 9:39
add a comment |
up vote
0
down vote
up vote
0
down vote
Here is a query using CONNECT BY that does the connection that you want and need.
From its results you have to select the first old_contract and the last new_contract.
SELECT NAME,OLD_CONTRACT,NEW_CONTRACT, LEVEL, rownum
FROM contract
START WITH OLD_CONTRACT = 3
CONNECT BY NOCYCLE PRIOR NEW_CONTRACT = OLD_CONTRACT
Results
NAME OLD_CONTRACT NEW_CONTRACT LEVEL ROWNUM
Nathaly 3 2 1 1
Gorge 2 8 2 2
Allen 8 20 3 3
Here is a query using CONNECT BY that does the connection that you want and need.
From its results you have to select the first old_contract and the last new_contract.
SELECT NAME,OLD_CONTRACT,NEW_CONTRACT, LEVEL, rownum
FROM contract
START WITH OLD_CONTRACT = 3
CONNECT BY NOCYCLE PRIOR NEW_CONTRACT = OLD_CONTRACT
Results
NAME OLD_CONTRACT NEW_CONTRACT LEVEL ROWNUM
Nathaly 3 2 1 1
Gorge 2 8 2 2
Allen 8 20 3 3
edited Nov 21 at 13:49
answered Nov 21 at 13:11
devdimi
2,2211616
2,2211616
Thank you! It canno't have "START WITH" because the are many keys at the same level. second , the key is varchar so there is no ordering. third , you didn't mention how to identify the "group" to take the first row and the last.
– Roni
Nov 22 at 9:39
add a comment |
Thank you! It canno't have "START WITH" because the are many keys at the same level. second , the key is varchar so there is no ordering. third , you didn't mention how to identify the "group" to take the first row and the last.
– Roni
Nov 22 at 9:39
Thank you! It canno't have "START WITH" because the are many keys at the same level. second , the key is varchar so there is no ordering. third , you didn't mention how to identify the "group" to take the first row and the last.
– Roni
Nov 22 at 9:39
Thank you! It canno't have "START WITH" because the are many keys at the same level. second , the key is varchar so there is no ordering. third , you didn't mention how to identify the "group" to take the first row and the last.
– Roni
Nov 22 at 9:39
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408150%2foracle-complex-hierarchy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Could you provide expected output? Only 2 columns
Old Key
andNew Key
?– Pham X. Bach
Nov 21 at 9:12
So what are the rules for deciding what value of
old_key
to start with? The first example1 -> 1
seems particularly troubling.– APC
Nov 21 at 11:05
"Could you provide expected output?" .... Yes. 2 Columns- Old key & New Key "So what are the rules for deciding what value..." There are many keys , there is no main Key
– Roni
Nov 22 at 9:33