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









share|improve this question




















  • 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















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









share|improve this question




















  • 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













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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












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
;





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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
    ;





    share|improve this answer

























      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
      ;





      share|improve this answer























        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
        ;





        share|improve this answer












        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
        ;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 16:24









        Felypp Oliveira

        1,676917




        1,676917






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python