Double Pivot across columns is failing












0















I wrote a code which has got two pivots in the query but it is failing to give any output.The error is : ORA-00904: "THESEUS_ACTIVE": invalid identifier.
Could anyone help me on this



SELECT company_id
, MAX(alt_rec_date) alt_rec_date
, rec_date
, MAX(STANDARD) STANDARD
, MAX(THESEUS) THESEUS
, CAST(NVL(MAX(STANDARD_Active), 0) AS NUMBER(1)) STANDARD_Active
, CAST(NVL(MAX(THESEUS_Active), 0) AS NUMBER(1)) THESEUS_Active --the
error is here in max(theseus_active)--
FROM (
**SELECT RH.company_id
,MAX(RH.alt_rec_date) alt_rec_date
,IT.type_code
,RH.alt_rec_code rec_code
,RH.rec_date
, CAST(ITC.active AS NUMBER(10)) active
,IT.type_code || '_Active' active_type_code
FROM tblInvestTypeRecHist RH
JOIN tblInvestType IT ON RH.type_code = IT.type_code
JOIN (
SELECT company_id, MAX(rec_date) rec_date
FROM tblInvestTypeRecHist GROUP BY company_id
) LR
ON RH.company_id = LR.company_id AND RH.rec_date = LR.rec_date
JOIN tblInvestTypeComp ITC
ON RH.company_id = ITC.company_id AND IT.type_code = ITC.type_code
GROUP BY RH.company_id
,IT.type_code
,RH.alt_rec_code
,RH.rec_date
,ITC.active**

) Data

PIVOT (
MAX(rec_code)
FOR type_code IN
('STANDARD','THESEUS')
) pvt

PIVOT (
MAX(active)
FOR active_type_code IN
('STANDARD_Active','THESEUS_Active')
) pvt

GROUP BY company_id, rec_date;


How can i resolve this?










share|improve this question



























    0















    I wrote a code which has got two pivots in the query but it is failing to give any output.The error is : ORA-00904: "THESEUS_ACTIVE": invalid identifier.
    Could anyone help me on this



    SELECT company_id
    , MAX(alt_rec_date) alt_rec_date
    , rec_date
    , MAX(STANDARD) STANDARD
    , MAX(THESEUS) THESEUS
    , CAST(NVL(MAX(STANDARD_Active), 0) AS NUMBER(1)) STANDARD_Active
    , CAST(NVL(MAX(THESEUS_Active), 0) AS NUMBER(1)) THESEUS_Active --the
    error is here in max(theseus_active)--
    FROM (
    **SELECT RH.company_id
    ,MAX(RH.alt_rec_date) alt_rec_date
    ,IT.type_code
    ,RH.alt_rec_code rec_code
    ,RH.rec_date
    , CAST(ITC.active AS NUMBER(10)) active
    ,IT.type_code || '_Active' active_type_code
    FROM tblInvestTypeRecHist RH
    JOIN tblInvestType IT ON RH.type_code = IT.type_code
    JOIN (
    SELECT company_id, MAX(rec_date) rec_date
    FROM tblInvestTypeRecHist GROUP BY company_id
    ) LR
    ON RH.company_id = LR.company_id AND RH.rec_date = LR.rec_date
    JOIN tblInvestTypeComp ITC
    ON RH.company_id = ITC.company_id AND IT.type_code = ITC.type_code
    GROUP BY RH.company_id
    ,IT.type_code
    ,RH.alt_rec_code
    ,RH.rec_date
    ,ITC.active**

    ) Data

    PIVOT (
    MAX(rec_code)
    FOR type_code IN
    ('STANDARD','THESEUS')
    ) pvt

    PIVOT (
    MAX(active)
    FOR active_type_code IN
    ('STANDARD_Active','THESEUS_Active')
    ) pvt

    GROUP BY company_id, rec_date;


    How can i resolve this?










    share|improve this question

























      0












      0








      0


      1






      I wrote a code which has got two pivots in the query but it is failing to give any output.The error is : ORA-00904: "THESEUS_ACTIVE": invalid identifier.
      Could anyone help me on this



      SELECT company_id
      , MAX(alt_rec_date) alt_rec_date
      , rec_date
      , MAX(STANDARD) STANDARD
      , MAX(THESEUS) THESEUS
      , CAST(NVL(MAX(STANDARD_Active), 0) AS NUMBER(1)) STANDARD_Active
      , CAST(NVL(MAX(THESEUS_Active), 0) AS NUMBER(1)) THESEUS_Active --the
      error is here in max(theseus_active)--
      FROM (
      **SELECT RH.company_id
      ,MAX(RH.alt_rec_date) alt_rec_date
      ,IT.type_code
      ,RH.alt_rec_code rec_code
      ,RH.rec_date
      , CAST(ITC.active AS NUMBER(10)) active
      ,IT.type_code || '_Active' active_type_code
      FROM tblInvestTypeRecHist RH
      JOIN tblInvestType IT ON RH.type_code = IT.type_code
      JOIN (
      SELECT company_id, MAX(rec_date) rec_date
      FROM tblInvestTypeRecHist GROUP BY company_id
      ) LR
      ON RH.company_id = LR.company_id AND RH.rec_date = LR.rec_date
      JOIN tblInvestTypeComp ITC
      ON RH.company_id = ITC.company_id AND IT.type_code = ITC.type_code
      GROUP BY RH.company_id
      ,IT.type_code
      ,RH.alt_rec_code
      ,RH.rec_date
      ,ITC.active**

      ) Data

      PIVOT (
      MAX(rec_code)
      FOR type_code IN
      ('STANDARD','THESEUS')
      ) pvt

      PIVOT (
      MAX(active)
      FOR active_type_code IN
      ('STANDARD_Active','THESEUS_Active')
      ) pvt

      GROUP BY company_id, rec_date;


      How can i resolve this?










      share|improve this question














      I wrote a code which has got two pivots in the query but it is failing to give any output.The error is : ORA-00904: "THESEUS_ACTIVE": invalid identifier.
      Could anyone help me on this



      SELECT company_id
      , MAX(alt_rec_date) alt_rec_date
      , rec_date
      , MAX(STANDARD) STANDARD
      , MAX(THESEUS) THESEUS
      , CAST(NVL(MAX(STANDARD_Active), 0) AS NUMBER(1)) STANDARD_Active
      , CAST(NVL(MAX(THESEUS_Active), 0) AS NUMBER(1)) THESEUS_Active --the
      error is here in max(theseus_active)--
      FROM (
      **SELECT RH.company_id
      ,MAX(RH.alt_rec_date) alt_rec_date
      ,IT.type_code
      ,RH.alt_rec_code rec_code
      ,RH.rec_date
      , CAST(ITC.active AS NUMBER(10)) active
      ,IT.type_code || '_Active' active_type_code
      FROM tblInvestTypeRecHist RH
      JOIN tblInvestType IT ON RH.type_code = IT.type_code
      JOIN (
      SELECT company_id, MAX(rec_date) rec_date
      FROM tblInvestTypeRecHist GROUP BY company_id
      ) LR
      ON RH.company_id = LR.company_id AND RH.rec_date = LR.rec_date
      JOIN tblInvestTypeComp ITC
      ON RH.company_id = ITC.company_id AND IT.type_code = ITC.type_code
      GROUP BY RH.company_id
      ,IT.type_code
      ,RH.alt_rec_code
      ,RH.rec_date
      ,ITC.active**

      ) Data

      PIVOT (
      MAX(rec_code)
      FOR type_code IN
      ('STANDARD','THESEUS')
      ) pvt

      PIVOT (
      MAX(active)
      FOR active_type_code IN
      ('STANDARD_Active','THESEUS_Active')
      ) pvt

      GROUP BY company_id, rec_date;


      How can i resolve this?







      oracle pivot






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 3 at 10:15









      vikkyvikky

      297




      297
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You aren't specifying aliases for the pivoted columns, so by default they end up as quoted identifiers set to the values being matched; so you can change the start of your query to:



          SELECT company_id
          , MAX(alt_rec_date) alt_rec_date
          , rec_date
          , MAX("'STANDARD'") STANDARD
          , MAX("'THESEUS'") THESEUS
          , CAST(NVL(MAX("'STANDARD_Active'"), 0) AS NUMBER(1)) STANDARD_Active
          , CAST(NVL(MAX("'THESEUS_Active'"), 0) AS NUMBER(1)) THESEUS_Active
          FROM (
          ...


          Notice both the double- and single-quotes, and the case exactly matching the generated string you produced earlier; and that you have to apply quotes to the columns from the first pivot as well as the second pivot.



          Alternatively, and probably more readably, give the pivoted columns aliases:



          SELECT company_id
          , MAX(alt_rec_date) alt_rec_date
          , rec_date
          , MAX(STANDARD) STANDARD
          , MAX(THESEUS) THESEUS
          , CAST(NVL(MAX(STANDARD_ACTIVE), 0) AS NUMBER(1)) STANDARD_Active
          , CAST(NVL(MAX(THESEUS_ACTIVE), 0) AS NUMBER(1)) THESEUS_Active
          FROM (
          ...
          ) Data

          PIVOT (
          MAX(rec_code)
          FOR type_code IN
          ('STANDARD' as standard,'THESEUS' as theseus)
          ) pvt

          PIVOT (
          MAX(active)
          FOR active_type_code IN
          ('STANDARD_Active' as standard_active,'THESEUS_Active' as theseus_active)
          ) pvt

          GROUP BY company_id, rec_date;




          I'm not sure you actually need two pivots here; without sample data and expected results I may well be missing something, but it looks like you might be able to do:



          ...
          ) Data
          PIVOT (
          MAX(rec_code), MAX(active) as active
          FOR type_code IN ('STANDARD' as standard,'THESEUS' as theseus)
          )
          GROUP BY company_id, rec_date;


          The whole thing looks like it could probably be simplified though.






          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',
            autoActivateHeartbeat: false,
            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%2f54020216%2fdouble-pivot-across-columns-is-failing%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









            0














            You aren't specifying aliases for the pivoted columns, so by default they end up as quoted identifiers set to the values being matched; so you can change the start of your query to:



            SELECT company_id
            , MAX(alt_rec_date) alt_rec_date
            , rec_date
            , MAX("'STANDARD'") STANDARD
            , MAX("'THESEUS'") THESEUS
            , CAST(NVL(MAX("'STANDARD_Active'"), 0) AS NUMBER(1)) STANDARD_Active
            , CAST(NVL(MAX("'THESEUS_Active'"), 0) AS NUMBER(1)) THESEUS_Active
            FROM (
            ...


            Notice both the double- and single-quotes, and the case exactly matching the generated string you produced earlier; and that you have to apply quotes to the columns from the first pivot as well as the second pivot.



            Alternatively, and probably more readably, give the pivoted columns aliases:



            SELECT company_id
            , MAX(alt_rec_date) alt_rec_date
            , rec_date
            , MAX(STANDARD) STANDARD
            , MAX(THESEUS) THESEUS
            , CAST(NVL(MAX(STANDARD_ACTIVE), 0) AS NUMBER(1)) STANDARD_Active
            , CAST(NVL(MAX(THESEUS_ACTIVE), 0) AS NUMBER(1)) THESEUS_Active
            FROM (
            ...
            ) Data

            PIVOT (
            MAX(rec_code)
            FOR type_code IN
            ('STANDARD' as standard,'THESEUS' as theseus)
            ) pvt

            PIVOT (
            MAX(active)
            FOR active_type_code IN
            ('STANDARD_Active' as standard_active,'THESEUS_Active' as theseus_active)
            ) pvt

            GROUP BY company_id, rec_date;




            I'm not sure you actually need two pivots here; without sample data and expected results I may well be missing something, but it looks like you might be able to do:



            ...
            ) Data
            PIVOT (
            MAX(rec_code), MAX(active) as active
            FOR type_code IN ('STANDARD' as standard,'THESEUS' as theseus)
            )
            GROUP BY company_id, rec_date;


            The whole thing looks like it could probably be simplified though.






            share|improve this answer






























              0














              You aren't specifying aliases for the pivoted columns, so by default they end up as quoted identifiers set to the values being matched; so you can change the start of your query to:



              SELECT company_id
              , MAX(alt_rec_date) alt_rec_date
              , rec_date
              , MAX("'STANDARD'") STANDARD
              , MAX("'THESEUS'") THESEUS
              , CAST(NVL(MAX("'STANDARD_Active'"), 0) AS NUMBER(1)) STANDARD_Active
              , CAST(NVL(MAX("'THESEUS_Active'"), 0) AS NUMBER(1)) THESEUS_Active
              FROM (
              ...


              Notice both the double- and single-quotes, and the case exactly matching the generated string you produced earlier; and that you have to apply quotes to the columns from the first pivot as well as the second pivot.



              Alternatively, and probably more readably, give the pivoted columns aliases:



              SELECT company_id
              , MAX(alt_rec_date) alt_rec_date
              , rec_date
              , MAX(STANDARD) STANDARD
              , MAX(THESEUS) THESEUS
              , CAST(NVL(MAX(STANDARD_ACTIVE), 0) AS NUMBER(1)) STANDARD_Active
              , CAST(NVL(MAX(THESEUS_ACTIVE), 0) AS NUMBER(1)) THESEUS_Active
              FROM (
              ...
              ) Data

              PIVOT (
              MAX(rec_code)
              FOR type_code IN
              ('STANDARD' as standard,'THESEUS' as theseus)
              ) pvt

              PIVOT (
              MAX(active)
              FOR active_type_code IN
              ('STANDARD_Active' as standard_active,'THESEUS_Active' as theseus_active)
              ) pvt

              GROUP BY company_id, rec_date;




              I'm not sure you actually need two pivots here; without sample data and expected results I may well be missing something, but it looks like you might be able to do:



              ...
              ) Data
              PIVOT (
              MAX(rec_code), MAX(active) as active
              FOR type_code IN ('STANDARD' as standard,'THESEUS' as theseus)
              )
              GROUP BY company_id, rec_date;


              The whole thing looks like it could probably be simplified though.






              share|improve this answer




























                0












                0








                0







                You aren't specifying aliases for the pivoted columns, so by default they end up as quoted identifiers set to the values being matched; so you can change the start of your query to:



                SELECT company_id
                , MAX(alt_rec_date) alt_rec_date
                , rec_date
                , MAX("'STANDARD'") STANDARD
                , MAX("'THESEUS'") THESEUS
                , CAST(NVL(MAX("'STANDARD_Active'"), 0) AS NUMBER(1)) STANDARD_Active
                , CAST(NVL(MAX("'THESEUS_Active'"), 0) AS NUMBER(1)) THESEUS_Active
                FROM (
                ...


                Notice both the double- and single-quotes, and the case exactly matching the generated string you produced earlier; and that you have to apply quotes to the columns from the first pivot as well as the second pivot.



                Alternatively, and probably more readably, give the pivoted columns aliases:



                SELECT company_id
                , MAX(alt_rec_date) alt_rec_date
                , rec_date
                , MAX(STANDARD) STANDARD
                , MAX(THESEUS) THESEUS
                , CAST(NVL(MAX(STANDARD_ACTIVE), 0) AS NUMBER(1)) STANDARD_Active
                , CAST(NVL(MAX(THESEUS_ACTIVE), 0) AS NUMBER(1)) THESEUS_Active
                FROM (
                ...
                ) Data

                PIVOT (
                MAX(rec_code)
                FOR type_code IN
                ('STANDARD' as standard,'THESEUS' as theseus)
                ) pvt

                PIVOT (
                MAX(active)
                FOR active_type_code IN
                ('STANDARD_Active' as standard_active,'THESEUS_Active' as theseus_active)
                ) pvt

                GROUP BY company_id, rec_date;




                I'm not sure you actually need two pivots here; without sample data and expected results I may well be missing something, but it looks like you might be able to do:



                ...
                ) Data
                PIVOT (
                MAX(rec_code), MAX(active) as active
                FOR type_code IN ('STANDARD' as standard,'THESEUS' as theseus)
                )
                GROUP BY company_id, rec_date;


                The whole thing looks like it could probably be simplified though.






                share|improve this answer















                You aren't specifying aliases for the pivoted columns, so by default they end up as quoted identifiers set to the values being matched; so you can change the start of your query to:



                SELECT company_id
                , MAX(alt_rec_date) alt_rec_date
                , rec_date
                , MAX("'STANDARD'") STANDARD
                , MAX("'THESEUS'") THESEUS
                , CAST(NVL(MAX("'STANDARD_Active'"), 0) AS NUMBER(1)) STANDARD_Active
                , CAST(NVL(MAX("'THESEUS_Active'"), 0) AS NUMBER(1)) THESEUS_Active
                FROM (
                ...


                Notice both the double- and single-quotes, and the case exactly matching the generated string you produced earlier; and that you have to apply quotes to the columns from the first pivot as well as the second pivot.



                Alternatively, and probably more readably, give the pivoted columns aliases:



                SELECT company_id
                , MAX(alt_rec_date) alt_rec_date
                , rec_date
                , MAX(STANDARD) STANDARD
                , MAX(THESEUS) THESEUS
                , CAST(NVL(MAX(STANDARD_ACTIVE), 0) AS NUMBER(1)) STANDARD_Active
                , CAST(NVL(MAX(THESEUS_ACTIVE), 0) AS NUMBER(1)) THESEUS_Active
                FROM (
                ...
                ) Data

                PIVOT (
                MAX(rec_code)
                FOR type_code IN
                ('STANDARD' as standard,'THESEUS' as theseus)
                ) pvt

                PIVOT (
                MAX(active)
                FOR active_type_code IN
                ('STANDARD_Active' as standard_active,'THESEUS_Active' as theseus_active)
                ) pvt

                GROUP BY company_id, rec_date;




                I'm not sure you actually need two pivots here; without sample data and expected results I may well be missing something, but it looks like you might be able to do:



                ...
                ) Data
                PIVOT (
                MAX(rec_code), MAX(active) as active
                FOR type_code IN ('STANDARD' as standard,'THESEUS' as theseus)
                )
                GROUP BY company_id, rec_date;


                The whole thing looks like it could probably be simplified though.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 4 at 11:00

























                answered Jan 3 at 10:55









                Alex PooleAlex Poole

                134k6108182




                134k6108182
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54020216%2fdouble-pivot-across-columns-is-failing%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

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas