Adding count columns based on criteria within sub query





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Hoping I can articulate this correctly. I have 3 tables within a Risk & Control database.



Table 1 contains details on all Risks and their likelihood of occurrence. Risk ID is a unique value :



Risk ID |Risk Desc |Impact  | Likelihood
4363 | XXX | High | Likely
2357 | XXX | Low | Unlikely
7629 | XXX | Medium | Unlikely
1929 | XXX | Low | Likely


Table 2 contains details on all Controls, and a rating on their effectiveness. Control ID is a unique value :



Control ID | Control Rating 
4566 | Effective
1431 | Not Effective
6724 | Effective
3415 | Effective


Table 3 contains a mapping of Risks with their linked controls in a 'many to many relationship' ie a risk can link to multiple controls and vice-versa.



Risk ID  | Control |
4363 | 4566 |
4363 | 1431 |
2357 | 4566 |
7629 | 6724 |


I am trying to create a view of all risk and an aggregated view of their control effectiveness, with 3 count columns. One that counts the total number of linked controls, one that counts the total number of EFFECTIVE controls and one that counts the total number of NOT EFFECTIVE controls.



The output would look like this :



Risk ID |Risk Desc |Impact  | Likelihood | Controls | Effective | Not Effective 
4363 | XXX | High | Likely | 2 | 1 | 1
2357 | XXX | Low | Unlikely | 1 | 1 | 0
7629 | XXX | Medium | Unlikely | 1 | 1 | 0
1929 | XXX | Low | Likely | 1 | 0 | 1


I have managed to get the first part, however not sure how I add the other counts?



SELECT Table_1.RISK_ID,
Table_1.RISK_DESC,
Table_1.Impact,
Table_1.Likelihood,
COUNT(TABLE_2.CONTROL_ID) AS Total_Controls
FROM Table_1
INNER JOIN Table_2
ON Table_1.RISK_ID = Table_2.RISK_ID
GROUP BY Table_1.RISK_ID,
Table_1.RISK_DESC,
Table_1.Impact,
Table_1.Likelihood


Any help greatly appreciated.










share|improve this question































    0















    Hoping I can articulate this correctly. I have 3 tables within a Risk & Control database.



    Table 1 contains details on all Risks and their likelihood of occurrence. Risk ID is a unique value :



    Risk ID |Risk Desc |Impact  | Likelihood
    4363 | XXX | High | Likely
    2357 | XXX | Low | Unlikely
    7629 | XXX | Medium | Unlikely
    1929 | XXX | Low | Likely


    Table 2 contains details on all Controls, and a rating on their effectiveness. Control ID is a unique value :



    Control ID | Control Rating 
    4566 | Effective
    1431 | Not Effective
    6724 | Effective
    3415 | Effective


    Table 3 contains a mapping of Risks with their linked controls in a 'many to many relationship' ie a risk can link to multiple controls and vice-versa.



    Risk ID  | Control |
    4363 | 4566 |
    4363 | 1431 |
    2357 | 4566 |
    7629 | 6724 |


    I am trying to create a view of all risk and an aggregated view of their control effectiveness, with 3 count columns. One that counts the total number of linked controls, one that counts the total number of EFFECTIVE controls and one that counts the total number of NOT EFFECTIVE controls.



    The output would look like this :



    Risk ID |Risk Desc |Impact  | Likelihood | Controls | Effective | Not Effective 
    4363 | XXX | High | Likely | 2 | 1 | 1
    2357 | XXX | Low | Unlikely | 1 | 1 | 0
    7629 | XXX | Medium | Unlikely | 1 | 1 | 0
    1929 | XXX | Low | Likely | 1 | 0 | 1


    I have managed to get the first part, however not sure how I add the other counts?



    SELECT Table_1.RISK_ID,
    Table_1.RISK_DESC,
    Table_1.Impact,
    Table_1.Likelihood,
    COUNT(TABLE_2.CONTROL_ID) AS Total_Controls
    FROM Table_1
    INNER JOIN Table_2
    ON Table_1.RISK_ID = Table_2.RISK_ID
    GROUP BY Table_1.RISK_ID,
    Table_1.RISK_DESC,
    Table_1.Impact,
    Table_1.Likelihood


    Any help greatly appreciated.










    share|improve this question



























      0












      0








      0








      Hoping I can articulate this correctly. I have 3 tables within a Risk & Control database.



      Table 1 contains details on all Risks and their likelihood of occurrence. Risk ID is a unique value :



      Risk ID |Risk Desc |Impact  | Likelihood
      4363 | XXX | High | Likely
      2357 | XXX | Low | Unlikely
      7629 | XXX | Medium | Unlikely
      1929 | XXX | Low | Likely


      Table 2 contains details on all Controls, and a rating on their effectiveness. Control ID is a unique value :



      Control ID | Control Rating 
      4566 | Effective
      1431 | Not Effective
      6724 | Effective
      3415 | Effective


      Table 3 contains a mapping of Risks with their linked controls in a 'many to many relationship' ie a risk can link to multiple controls and vice-versa.



      Risk ID  | Control |
      4363 | 4566 |
      4363 | 1431 |
      2357 | 4566 |
      7629 | 6724 |


      I am trying to create a view of all risk and an aggregated view of their control effectiveness, with 3 count columns. One that counts the total number of linked controls, one that counts the total number of EFFECTIVE controls and one that counts the total number of NOT EFFECTIVE controls.



      The output would look like this :



      Risk ID |Risk Desc |Impact  | Likelihood | Controls | Effective | Not Effective 
      4363 | XXX | High | Likely | 2 | 1 | 1
      2357 | XXX | Low | Unlikely | 1 | 1 | 0
      7629 | XXX | Medium | Unlikely | 1 | 1 | 0
      1929 | XXX | Low | Likely | 1 | 0 | 1


      I have managed to get the first part, however not sure how I add the other counts?



      SELECT Table_1.RISK_ID,
      Table_1.RISK_DESC,
      Table_1.Impact,
      Table_1.Likelihood,
      COUNT(TABLE_2.CONTROL_ID) AS Total_Controls
      FROM Table_1
      INNER JOIN Table_2
      ON Table_1.RISK_ID = Table_2.RISK_ID
      GROUP BY Table_1.RISK_ID,
      Table_1.RISK_DESC,
      Table_1.Impact,
      Table_1.Likelihood


      Any help greatly appreciated.










      share|improve this question
















      Hoping I can articulate this correctly. I have 3 tables within a Risk & Control database.



      Table 1 contains details on all Risks and their likelihood of occurrence. Risk ID is a unique value :



      Risk ID |Risk Desc |Impact  | Likelihood
      4363 | XXX | High | Likely
      2357 | XXX | Low | Unlikely
      7629 | XXX | Medium | Unlikely
      1929 | XXX | Low | Likely


      Table 2 contains details on all Controls, and a rating on their effectiveness. Control ID is a unique value :



      Control ID | Control Rating 
      4566 | Effective
      1431 | Not Effective
      6724 | Effective
      3415 | Effective


      Table 3 contains a mapping of Risks with their linked controls in a 'many to many relationship' ie a risk can link to multiple controls and vice-versa.



      Risk ID  | Control |
      4363 | 4566 |
      4363 | 1431 |
      2357 | 4566 |
      7629 | 6724 |


      I am trying to create a view of all risk and an aggregated view of their control effectiveness, with 3 count columns. One that counts the total number of linked controls, one that counts the total number of EFFECTIVE controls and one that counts the total number of NOT EFFECTIVE controls.



      The output would look like this :



      Risk ID |Risk Desc |Impact  | Likelihood | Controls | Effective | Not Effective 
      4363 | XXX | High | Likely | 2 | 1 | 1
      2357 | XXX | Low | Unlikely | 1 | 1 | 0
      7629 | XXX | Medium | Unlikely | 1 | 1 | 0
      1929 | XXX | Low | Likely | 1 | 0 | 1


      I have managed to get the first part, however not sure how I add the other counts?



      SELECT Table_1.RISK_ID,
      Table_1.RISK_DESC,
      Table_1.Impact,
      Table_1.Likelihood,
      COUNT(TABLE_2.CONTROL_ID) AS Total_Controls
      FROM Table_1
      INNER JOIN Table_2
      ON Table_1.RISK_ID = Table_2.RISK_ID
      GROUP BY Table_1.RISK_ID,
      Table_1.RISK_DESC,
      Table_1.Impact,
      Table_1.Likelihood


      Any help greatly appreciated.







      sql oracle count






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 17:47









      William Robertson

      8,51732233




      8,51732233










      asked Nov 23 '18 at 16:18









      ScottCeeScottCee

      314




      314
























          2 Answers
          2






          active

          oldest

          votes


















          0














          I'm not really sure if this is correct. In your sample tables you have no risk id in table two but your example shows you joining table one and table two on risk_id? So i'll just write this assuming your sample tables are correct.



              SELECT Table_1.RISK_ID,
          Table_1.RISK_DESC,
          Table_1.Impact,
          Table_1.Likelihood,
          COUNT(TABLE_2.CONTROL_ID) AS Total_Controls,
          COUNT(CASE WHEN CONTROL_RATING IN ('Effective') THEN 'x' END) as effective,
          COUNT(CASE WHEN CONTROL_RATING IN ('Not Effective') THEN 'x' END) as not_effective
          FROM Table_1
          INNER JOIN Table_3
          ON Table_3.Risk_ID = Table_1.Risk_ID
          INNER JOIN Table_2
          ON Table_2.CONTROL_ID = Table_3.CONTROL
          GROUP BY Table_1.RISK_ID,
          Table_1.RISK_DESC,
          Table_1.Impact,
          Table_1.Likelihood


          Sum will also work in this, but you wanted COUNT()






          share|improve this answer
























          • Thanks K.Layton - my mistake when giving the sample table. Your solution has worked perfectly!

            – ScottCee
            Nov 26 '18 at 8:43



















          0














          You could use simple math and aggregation for your "effective" and "not effective" columns. Pseudo Code:



          Controls = COUNT(*)



          Effective = SUM(CASE WHEN Control_Rating="Effective" THEN 1 ELSE 0 END)



          Not Effective = SUM(CASE WHEN Control_Rating="Not Effective" THEN 1 ELSE 0 END)






          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%2f53449984%2fadding-count-columns-based-on-criteria-within-sub-query%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I'm not really sure if this is correct. In your sample tables you have no risk id in table two but your example shows you joining table one and table two on risk_id? So i'll just write this assuming your sample tables are correct.



                SELECT Table_1.RISK_ID,
            Table_1.RISK_DESC,
            Table_1.Impact,
            Table_1.Likelihood,
            COUNT(TABLE_2.CONTROL_ID) AS Total_Controls,
            COUNT(CASE WHEN CONTROL_RATING IN ('Effective') THEN 'x' END) as effective,
            COUNT(CASE WHEN CONTROL_RATING IN ('Not Effective') THEN 'x' END) as not_effective
            FROM Table_1
            INNER JOIN Table_3
            ON Table_3.Risk_ID = Table_1.Risk_ID
            INNER JOIN Table_2
            ON Table_2.CONTROL_ID = Table_3.CONTROL
            GROUP BY Table_1.RISK_ID,
            Table_1.RISK_DESC,
            Table_1.Impact,
            Table_1.Likelihood


            Sum will also work in this, but you wanted COUNT()






            share|improve this answer
























            • Thanks K.Layton - my mistake when giving the sample table. Your solution has worked perfectly!

              – ScottCee
              Nov 26 '18 at 8:43
















            0














            I'm not really sure if this is correct. In your sample tables you have no risk id in table two but your example shows you joining table one and table two on risk_id? So i'll just write this assuming your sample tables are correct.



                SELECT Table_1.RISK_ID,
            Table_1.RISK_DESC,
            Table_1.Impact,
            Table_1.Likelihood,
            COUNT(TABLE_2.CONTROL_ID) AS Total_Controls,
            COUNT(CASE WHEN CONTROL_RATING IN ('Effective') THEN 'x' END) as effective,
            COUNT(CASE WHEN CONTROL_RATING IN ('Not Effective') THEN 'x' END) as not_effective
            FROM Table_1
            INNER JOIN Table_3
            ON Table_3.Risk_ID = Table_1.Risk_ID
            INNER JOIN Table_2
            ON Table_2.CONTROL_ID = Table_3.CONTROL
            GROUP BY Table_1.RISK_ID,
            Table_1.RISK_DESC,
            Table_1.Impact,
            Table_1.Likelihood


            Sum will also work in this, but you wanted COUNT()






            share|improve this answer
























            • Thanks K.Layton - my mistake when giving the sample table. Your solution has worked perfectly!

              – ScottCee
              Nov 26 '18 at 8:43














            0












            0








            0







            I'm not really sure if this is correct. In your sample tables you have no risk id in table two but your example shows you joining table one and table two on risk_id? So i'll just write this assuming your sample tables are correct.



                SELECT Table_1.RISK_ID,
            Table_1.RISK_DESC,
            Table_1.Impact,
            Table_1.Likelihood,
            COUNT(TABLE_2.CONTROL_ID) AS Total_Controls,
            COUNT(CASE WHEN CONTROL_RATING IN ('Effective') THEN 'x' END) as effective,
            COUNT(CASE WHEN CONTROL_RATING IN ('Not Effective') THEN 'x' END) as not_effective
            FROM Table_1
            INNER JOIN Table_3
            ON Table_3.Risk_ID = Table_1.Risk_ID
            INNER JOIN Table_2
            ON Table_2.CONTROL_ID = Table_3.CONTROL
            GROUP BY Table_1.RISK_ID,
            Table_1.RISK_DESC,
            Table_1.Impact,
            Table_1.Likelihood


            Sum will also work in this, but you wanted COUNT()






            share|improve this answer













            I'm not really sure if this is correct. In your sample tables you have no risk id in table two but your example shows you joining table one and table two on risk_id? So i'll just write this assuming your sample tables are correct.



                SELECT Table_1.RISK_ID,
            Table_1.RISK_DESC,
            Table_1.Impact,
            Table_1.Likelihood,
            COUNT(TABLE_2.CONTROL_ID) AS Total_Controls,
            COUNT(CASE WHEN CONTROL_RATING IN ('Effective') THEN 'x' END) as effective,
            COUNT(CASE WHEN CONTROL_RATING IN ('Not Effective') THEN 'x' END) as not_effective
            FROM Table_1
            INNER JOIN Table_3
            ON Table_3.Risk_ID = Table_1.Risk_ID
            INNER JOIN Table_2
            ON Table_2.CONTROL_ID = Table_3.CONTROL
            GROUP BY Table_1.RISK_ID,
            Table_1.RISK_DESC,
            Table_1.Impact,
            Table_1.Likelihood


            Sum will also work in this, but you wanted COUNT()







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 23 '18 at 17:21









            K. LaytonK. Layton

            728




            728













            • Thanks K.Layton - my mistake when giving the sample table. Your solution has worked perfectly!

              – ScottCee
              Nov 26 '18 at 8:43



















            • Thanks K.Layton - my mistake when giving the sample table. Your solution has worked perfectly!

              – ScottCee
              Nov 26 '18 at 8:43

















            Thanks K.Layton - my mistake when giving the sample table. Your solution has worked perfectly!

            – ScottCee
            Nov 26 '18 at 8:43





            Thanks K.Layton - my mistake when giving the sample table. Your solution has worked perfectly!

            – ScottCee
            Nov 26 '18 at 8:43













            0














            You could use simple math and aggregation for your "effective" and "not effective" columns. Pseudo Code:



            Controls = COUNT(*)



            Effective = SUM(CASE WHEN Control_Rating="Effective" THEN 1 ELSE 0 END)



            Not Effective = SUM(CASE WHEN Control_Rating="Not Effective" THEN 1 ELSE 0 END)






            share|improve this answer




























              0














              You could use simple math and aggregation for your "effective" and "not effective" columns. Pseudo Code:



              Controls = COUNT(*)



              Effective = SUM(CASE WHEN Control_Rating="Effective" THEN 1 ELSE 0 END)



              Not Effective = SUM(CASE WHEN Control_Rating="Not Effective" THEN 1 ELSE 0 END)






              share|improve this answer


























                0












                0








                0







                You could use simple math and aggregation for your "effective" and "not effective" columns. Pseudo Code:



                Controls = COUNT(*)



                Effective = SUM(CASE WHEN Control_Rating="Effective" THEN 1 ELSE 0 END)



                Not Effective = SUM(CASE WHEN Control_Rating="Not Effective" THEN 1 ELSE 0 END)






                share|improve this answer













                You could use simple math and aggregation for your "effective" and "not effective" columns. Pseudo Code:



                Controls = COUNT(*)



                Effective = SUM(CASE WHEN Control_Rating="Effective" THEN 1 ELSE 0 END)



                Not Effective = SUM(CASE WHEN Control_Rating="Not Effective" THEN 1 ELSE 0 END)







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 16:24









                The_Data_DoctorThe_Data_Doctor

                114




                114






























                    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%2f53449984%2fadding-count-columns-based-on-criteria-within-sub-query%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







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini