Oracle SQL dynamic pivot
up vote
0
down vote
favorite
Imagine there are two tables table like so:
Table 1
ID | NAME | ATTR_A | ATTR_B | ATTR_C | ATTR_D
------------------------------------------------------------------
0 Bob 1 2 3 0
1 Jane 2 0 4 3
2 Richard 1 3 5 4
Table 2
ID | TABLE_1_FK | ATTR_E
--------------------------------
0 0 A
1 1 B
2 1 C
3 1 D
4 2 E
5 2 F
Using a left join to merge table 1 and 2, the resulting table will be
Joined Table
NAME | ATTR_E | ATTR_A | ATTR_B | ATTR_C | ATTR_D
-----------------------------------------------------------------------
Bob A 1 2 3 0
Jane B 2 0 4 3
Jane C 2 0 4 3
Jane D 2 0 4 3
Richard E 1 3 5 4
Richard F 1 3 5 4
Question
3 out of the 4 attributes (ATTR_A,B,C,D) in table 1 will always have a value greater than 0, create a new table so that if the attribute in table 1 has a value greater than 0, display it under a "table 1 attr" column, with its value under a "table 1 attr_value" column. Along with that, create a new column to describe the attribute selected.
The resulting table would be like so:
Result
NAME | ATTR_E | "table 1 attr" | "table 1 value" | "attr description"
---------------------------------------------------------------------------------------------
Bob A ATTR_A 1 Apple
Bob A ATTR_B 2 Banana
Bob A ATTR_C 3 Carrot
Jane B ATTR_A 2 Apple
Jane B ATTR_C 4 Carrot
Jane B ATTR_D 3 Durian
Jane C ATTR_A 2 Apple
Jane C ATTR_C 4 Carrot
Jane C ATTR_D 3 Durian
Jane D ATTR_A 2 Apple
Jane D ATTR_C 4 Carrot
Jane D ATTR_D 3 Durian
Richard E ATTR_A 1 Apple
Richard E ATTR_B 3 Banana
Richard E ATTR_C 5 Carrot
Richard E ATTR_D 4 Durian
Richard F ATTR_A 1 Apple
Richard F ATTR_B 3 Banana
Richard F ATTR_C 5 Carrot
Richard F ATTR_D 4 Durian
sql database oracle
add a comment |
up vote
0
down vote
favorite
Imagine there are two tables table like so:
Table 1
ID | NAME | ATTR_A | ATTR_B | ATTR_C | ATTR_D
------------------------------------------------------------------
0 Bob 1 2 3 0
1 Jane 2 0 4 3
2 Richard 1 3 5 4
Table 2
ID | TABLE_1_FK | ATTR_E
--------------------------------
0 0 A
1 1 B
2 1 C
3 1 D
4 2 E
5 2 F
Using a left join to merge table 1 and 2, the resulting table will be
Joined Table
NAME | ATTR_E | ATTR_A | ATTR_B | ATTR_C | ATTR_D
-----------------------------------------------------------------------
Bob A 1 2 3 0
Jane B 2 0 4 3
Jane C 2 0 4 3
Jane D 2 0 4 3
Richard E 1 3 5 4
Richard F 1 3 5 4
Question
3 out of the 4 attributes (ATTR_A,B,C,D) in table 1 will always have a value greater than 0, create a new table so that if the attribute in table 1 has a value greater than 0, display it under a "table 1 attr" column, with its value under a "table 1 attr_value" column. Along with that, create a new column to describe the attribute selected.
The resulting table would be like so:
Result
NAME | ATTR_E | "table 1 attr" | "table 1 value" | "attr description"
---------------------------------------------------------------------------------------------
Bob A ATTR_A 1 Apple
Bob A ATTR_B 2 Banana
Bob A ATTR_C 3 Carrot
Jane B ATTR_A 2 Apple
Jane B ATTR_C 4 Carrot
Jane B ATTR_D 3 Durian
Jane C ATTR_A 2 Apple
Jane C ATTR_C 4 Carrot
Jane C ATTR_D 3 Durian
Jane D ATTR_A 2 Apple
Jane D ATTR_C 4 Carrot
Jane D ATTR_D 3 Durian
Richard E ATTR_A 1 Apple
Richard E ATTR_B 3 Banana
Richard E ATTR_C 5 Carrot
Richard E ATTR_D 4 Durian
Richard F ATTR_A 1 Apple
Richard F ATTR_B 3 Banana
Richard F ATTR_C 5 Carrot
Richard F ATTR_D 4 Durian
sql database oracle
1
What have you tried so far?
– Josh Eller
Nov 19 at 16:15
I'm really confused. Where does the description come from?
– Gordon Linoff
Nov 19 at 16:24
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Imagine there are two tables table like so:
Table 1
ID | NAME | ATTR_A | ATTR_B | ATTR_C | ATTR_D
------------------------------------------------------------------
0 Bob 1 2 3 0
1 Jane 2 0 4 3
2 Richard 1 3 5 4
Table 2
ID | TABLE_1_FK | ATTR_E
--------------------------------
0 0 A
1 1 B
2 1 C
3 1 D
4 2 E
5 2 F
Using a left join to merge table 1 and 2, the resulting table will be
Joined Table
NAME | ATTR_E | ATTR_A | ATTR_B | ATTR_C | ATTR_D
-----------------------------------------------------------------------
Bob A 1 2 3 0
Jane B 2 0 4 3
Jane C 2 0 4 3
Jane D 2 0 4 3
Richard E 1 3 5 4
Richard F 1 3 5 4
Question
3 out of the 4 attributes (ATTR_A,B,C,D) in table 1 will always have a value greater than 0, create a new table so that if the attribute in table 1 has a value greater than 0, display it under a "table 1 attr" column, with its value under a "table 1 attr_value" column. Along with that, create a new column to describe the attribute selected.
The resulting table would be like so:
Result
NAME | ATTR_E | "table 1 attr" | "table 1 value" | "attr description"
---------------------------------------------------------------------------------------------
Bob A ATTR_A 1 Apple
Bob A ATTR_B 2 Banana
Bob A ATTR_C 3 Carrot
Jane B ATTR_A 2 Apple
Jane B ATTR_C 4 Carrot
Jane B ATTR_D 3 Durian
Jane C ATTR_A 2 Apple
Jane C ATTR_C 4 Carrot
Jane C ATTR_D 3 Durian
Jane D ATTR_A 2 Apple
Jane D ATTR_C 4 Carrot
Jane D ATTR_D 3 Durian
Richard E ATTR_A 1 Apple
Richard E ATTR_B 3 Banana
Richard E ATTR_C 5 Carrot
Richard E ATTR_D 4 Durian
Richard F ATTR_A 1 Apple
Richard F ATTR_B 3 Banana
Richard F ATTR_C 5 Carrot
Richard F ATTR_D 4 Durian
sql database oracle
Imagine there are two tables table like so:
Table 1
ID | NAME | ATTR_A | ATTR_B | ATTR_C | ATTR_D
------------------------------------------------------------------
0 Bob 1 2 3 0
1 Jane 2 0 4 3
2 Richard 1 3 5 4
Table 2
ID | TABLE_1_FK | ATTR_E
--------------------------------
0 0 A
1 1 B
2 1 C
3 1 D
4 2 E
5 2 F
Using a left join to merge table 1 and 2, the resulting table will be
Joined Table
NAME | ATTR_E | ATTR_A | ATTR_B | ATTR_C | ATTR_D
-----------------------------------------------------------------------
Bob A 1 2 3 0
Jane B 2 0 4 3
Jane C 2 0 4 3
Jane D 2 0 4 3
Richard E 1 3 5 4
Richard F 1 3 5 4
Question
3 out of the 4 attributes (ATTR_A,B,C,D) in table 1 will always have a value greater than 0, create a new table so that if the attribute in table 1 has a value greater than 0, display it under a "table 1 attr" column, with its value under a "table 1 attr_value" column. Along with that, create a new column to describe the attribute selected.
The resulting table would be like so:
Result
NAME | ATTR_E | "table 1 attr" | "table 1 value" | "attr description"
---------------------------------------------------------------------------------------------
Bob A ATTR_A 1 Apple
Bob A ATTR_B 2 Banana
Bob A ATTR_C 3 Carrot
Jane B ATTR_A 2 Apple
Jane B ATTR_C 4 Carrot
Jane B ATTR_D 3 Durian
Jane C ATTR_A 2 Apple
Jane C ATTR_C 4 Carrot
Jane C ATTR_D 3 Durian
Jane D ATTR_A 2 Apple
Jane D ATTR_C 4 Carrot
Jane D ATTR_D 3 Durian
Richard E ATTR_A 1 Apple
Richard E ATTR_B 3 Banana
Richard E ATTR_C 5 Carrot
Richard E ATTR_D 4 Durian
Richard F ATTR_A 1 Apple
Richard F ATTR_B 3 Banana
Richard F ATTR_C 5 Carrot
Richard F ATTR_D 4 Durian
sql database oracle
sql database oracle
edited Nov 19 at 16:13
asked Nov 19 at 16:05
Dowopq
32
32
1
What have you tried so far?
– Josh Eller
Nov 19 at 16:15
I'm really confused. Where does the description come from?
– Gordon Linoff
Nov 19 at 16:24
add a comment |
1
What have you tried so far?
– Josh Eller
Nov 19 at 16:15
I'm really confused. Where does the description come from?
– Gordon Linoff
Nov 19 at 16:24
1
1
What have you tried so far?
– Josh Eller
Nov 19 at 16:15
What have you tried so far?
– Josh Eller
Nov 19 at 16:15
I'm really confused. Where does the description come from?
– Gordon Linoff
Nov 19 at 16:24
I'm really confused. Where does the description come from?
– Gordon Linoff
Nov 19 at 16:24
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
You need to UNPIVOT
your joined result. I'm posting a working example exactly like your problem, so you replace with your tables and such as stated in the sql comments.
SELECT *
FROM
(
-- your join result here...
SELECT 1 col_id, 2 col_attrib_a, 0 col_attrib_b FROM dual UNION
SELECT 9 col_id, 0 col_attrib_a, 7 col_attrib_b FROM dual
)
UNPIVOT
(
value
FOR value_type IN (col_attrib_a, col_attrib_b) -- the name of the attrib columns here...
)
WHERE 1=1
AND value > 0
;
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
accepted
You need to UNPIVOT
your joined result. I'm posting a working example exactly like your problem, so you replace with your tables and such as stated in the sql comments.
SELECT *
FROM
(
-- your join result here...
SELECT 1 col_id, 2 col_attrib_a, 0 col_attrib_b FROM dual UNION
SELECT 9 col_id, 0 col_attrib_a, 7 col_attrib_b FROM dual
)
UNPIVOT
(
value
FOR value_type IN (col_attrib_a, col_attrib_b) -- the name of the attrib columns here...
)
WHERE 1=1
AND value > 0
;
add a comment |
up vote
0
down vote
accepted
You need to UNPIVOT
your joined result. I'm posting a working example exactly like your problem, so you replace with your tables and such as stated in the sql comments.
SELECT *
FROM
(
-- your join result here...
SELECT 1 col_id, 2 col_attrib_a, 0 col_attrib_b FROM dual UNION
SELECT 9 col_id, 0 col_attrib_a, 7 col_attrib_b FROM dual
)
UNPIVOT
(
value
FOR value_type IN (col_attrib_a, col_attrib_b) -- the name of the attrib columns here...
)
WHERE 1=1
AND value > 0
;
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You need to UNPIVOT
your joined result. I'm posting a working example exactly like your problem, so you replace with your tables and such as stated in the sql comments.
SELECT *
FROM
(
-- your join result here...
SELECT 1 col_id, 2 col_attrib_a, 0 col_attrib_b FROM dual UNION
SELECT 9 col_id, 0 col_attrib_a, 7 col_attrib_b FROM dual
)
UNPIVOT
(
value
FOR value_type IN (col_attrib_a, col_attrib_b) -- the name of the attrib columns here...
)
WHERE 1=1
AND value > 0
;
You need to UNPIVOT
your joined result. I'm posting a working example exactly like your problem, so you replace with your tables and such as stated in the sql comments.
SELECT *
FROM
(
-- your join result here...
SELECT 1 col_id, 2 col_attrib_a, 0 col_attrib_b FROM dual UNION
SELECT 9 col_id, 0 col_attrib_a, 7 col_attrib_b FROM dual
)
UNPIVOT
(
value
FOR value_type IN (col_attrib_a, col_attrib_b) -- the name of the attrib columns here...
)
WHERE 1=1
AND value > 0
;
answered Nov 19 at 16:24
Felypp Oliveira
1,676917
1,676917
add a comment |
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%2f53378529%2foracle-sql-dynamic-pivot%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
1
What have you tried so far?
– Josh Eller
Nov 19 at 16:15
I'm really confused. Where does the description come from?
– Gordon Linoff
Nov 19 at 16:24