Count rows with specific entry for every distinct entry of another row












1














So my task is to calculate some simple KPIs.



I have already accumulated a view with all the data I need.



 Year_CW    Is Started      Needs Help
-------------------------------------
2018/45 0 1
2018/43 1 1
2018/45 0 1
2018/42 1 0
2018/45 0 1
2018/45 1 1
2018/41 0 1
2018/43 0 0
2018/45 1 1
2018/45 0 0


I then wrote the following query:



SELECT DISTINCT YEAR_CW 
FROM TestView
ORDER BY YEAR_CW DESC


Which returns this



Year_CW
--------
2018/45
2018/44
2018/43
2018/42


I would now like to count for each Year_CW how often there is a 1 and how often there is a zero for both of the other rows. This may be a fairly simple question but I'm just starting with SQL and I really don't know what the keyword is for a query based on an outer query.



The other queries would be



Select Count(Is Started)
from Testview
Where Is Started = 1


And so on for the others. But I really don't know how to put them together and base them on the first query.



Thanks for your help.










share|improve this question





























    1














    So my task is to calculate some simple KPIs.



    I have already accumulated a view with all the data I need.



     Year_CW    Is Started      Needs Help
    -------------------------------------
    2018/45 0 1
    2018/43 1 1
    2018/45 0 1
    2018/42 1 0
    2018/45 0 1
    2018/45 1 1
    2018/41 0 1
    2018/43 0 0
    2018/45 1 1
    2018/45 0 0


    I then wrote the following query:



    SELECT DISTINCT YEAR_CW 
    FROM TestView
    ORDER BY YEAR_CW DESC


    Which returns this



    Year_CW
    --------
    2018/45
    2018/44
    2018/43
    2018/42


    I would now like to count for each Year_CW how often there is a 1 and how often there is a zero for both of the other rows. This may be a fairly simple question but I'm just starting with SQL and I really don't know what the keyword is for a query based on an outer query.



    The other queries would be



    Select Count(Is Started)
    from Testview
    Where Is Started = 1


    And so on for the others. But I really don't know how to put them together and base them on the first query.



    Thanks for your help.










    share|improve this question



























      1












      1








      1







      So my task is to calculate some simple KPIs.



      I have already accumulated a view with all the data I need.



       Year_CW    Is Started      Needs Help
      -------------------------------------
      2018/45 0 1
      2018/43 1 1
      2018/45 0 1
      2018/42 1 0
      2018/45 0 1
      2018/45 1 1
      2018/41 0 1
      2018/43 0 0
      2018/45 1 1
      2018/45 0 0


      I then wrote the following query:



      SELECT DISTINCT YEAR_CW 
      FROM TestView
      ORDER BY YEAR_CW DESC


      Which returns this



      Year_CW
      --------
      2018/45
      2018/44
      2018/43
      2018/42


      I would now like to count for each Year_CW how often there is a 1 and how often there is a zero for both of the other rows. This may be a fairly simple question but I'm just starting with SQL and I really don't know what the keyword is for a query based on an outer query.



      The other queries would be



      Select Count(Is Started)
      from Testview
      Where Is Started = 1


      And so on for the others. But I really don't know how to put them together and base them on the first query.



      Thanks for your help.










      share|improve this question















      So my task is to calculate some simple KPIs.



      I have already accumulated a view with all the data I need.



       Year_CW    Is Started      Needs Help
      -------------------------------------
      2018/45 0 1
      2018/43 1 1
      2018/45 0 1
      2018/42 1 0
      2018/45 0 1
      2018/45 1 1
      2018/41 0 1
      2018/43 0 0
      2018/45 1 1
      2018/45 0 0


      I then wrote the following query:



      SELECT DISTINCT YEAR_CW 
      FROM TestView
      ORDER BY YEAR_CW DESC


      Which returns this



      Year_CW
      --------
      2018/45
      2018/44
      2018/43
      2018/42


      I would now like to count for each Year_CW how often there is a 1 and how often there is a zero for both of the other rows. This may be a fairly simple question but I'm just starting with SQL and I really don't know what the keyword is for a query based on an outer query.



      The other queries would be



      Select Count(Is Started)
      from Testview
      Where Is Started = 1


      And so on for the others. But I really don't know how to put them together and base them on the first query.



      Thanks for your help.







      sql oracle11g






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 20:34









      marc_s

      571k12811031252




      571k12811031252










      asked Nov 21 '18 at 13:12









      Odatas

      404




      404
























          4 Answers
          4






          active

          oldest

          votes


















          0














          You seems want conditional aggregation :



          select Year_CW,
          sum(case when col = 1 then 1 else 0 end) as one_count,
          sum(case when col = 0 then 1 else 0 end) as zero_count
          from (select Year_CW, IsStarted as col
          from TestView tv
          union all
          select Year_CW, NeedsHelp
          from TestView tv
          ) tv
          group by Year_CW
          order by Year_CW desc;





          share|improve this answer























          • This does the work. Is there a way to sort the group "Year_CW" descending?
            – Odatas
            Nov 21 '18 at 13:31










          • @Odatas. . . You can do order by Year_CW desc.
            – Yogesh Sharma
            Nov 21 '18 at 13:32



















          1














          select Year_CW
          , sum(case when Is_Started = 1 then 1 end) as Is_Started_1
          , sum(case when Is_Started = 0 then 1 end) as Is_Started_0
          , sum(case when Needs_Help = 1 then 1 end) as Needs_Help_1
          , sum(case when Needs_Help = 0 then 1 end) as Needs_Help_0
          from Test_View
          group by Year_CW


          So how I did it is I created 4 new fields for you. First one is giving value ‘1’ to every field where ‘Is_Started = 1’ and then I sum the instances. I did the same for 0 values and another two fields for values 1 and 0 for ‘Needs_ Help’ column. I believe this will give you your desired result.






          share|improve this answer























          • Adding an explanation of the query provided would be good to help others understand what you have done.
            – Dessus
            Nov 21 '18 at 21:02










          • Comment added brother. Sorry.
            – Filip Kubiak
            Nov 21 '18 at 21:09



















          0














          So, if I'm understanding the question correctly you're just looking for the SUM of the additional two columns GROUP BY the Year_CW field. That would be the following.



          SELECT Year_CW, SUM([Is Started]), SUM([Needs Help])
          FROM TestView
          GROUP BY Year_CW





          share|improve this answer





















          • Not just the sum (This would only work for 1s) but the number of times specific entrys appear. But the "Group by" is the thing i was searching for.
            – Odatas
            Nov 21 '18 at 13:32



















          0














          If it's only 0 or 1, then both 0 or 1 can be summed up.



          SELECT YEAR_CW, 
          SUM("Is Started") AS TotalStarted,
          SUM(1 - "Is Started") AS TotalNotStarted,
          SUM("Needs Help") AS TotalNeedsHelp,
          SUM(1 - "Needs Help") AS TotalNoHelpNeeded
          FROM TestView
          GROUP BY YEAR_CW
          ORDER BY YEAR_CW DESC





          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%2f53412834%2fcount-rows-with-specific-entry-for-every-distinct-entry-of-another-row%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You seems want conditional aggregation :



            select Year_CW,
            sum(case when col = 1 then 1 else 0 end) as one_count,
            sum(case when col = 0 then 1 else 0 end) as zero_count
            from (select Year_CW, IsStarted as col
            from TestView tv
            union all
            select Year_CW, NeedsHelp
            from TestView tv
            ) tv
            group by Year_CW
            order by Year_CW desc;





            share|improve this answer























            • This does the work. Is there a way to sort the group "Year_CW" descending?
              – Odatas
              Nov 21 '18 at 13:31










            • @Odatas. . . You can do order by Year_CW desc.
              – Yogesh Sharma
              Nov 21 '18 at 13:32
















            0














            You seems want conditional aggregation :



            select Year_CW,
            sum(case when col = 1 then 1 else 0 end) as one_count,
            sum(case when col = 0 then 1 else 0 end) as zero_count
            from (select Year_CW, IsStarted as col
            from TestView tv
            union all
            select Year_CW, NeedsHelp
            from TestView tv
            ) tv
            group by Year_CW
            order by Year_CW desc;





            share|improve this answer























            • This does the work. Is there a way to sort the group "Year_CW" descending?
              – Odatas
              Nov 21 '18 at 13:31










            • @Odatas. . . You can do order by Year_CW desc.
              – Yogesh Sharma
              Nov 21 '18 at 13:32














            0












            0








            0






            You seems want conditional aggregation :



            select Year_CW,
            sum(case when col = 1 then 1 else 0 end) as one_count,
            sum(case when col = 0 then 1 else 0 end) as zero_count
            from (select Year_CW, IsStarted as col
            from TestView tv
            union all
            select Year_CW, NeedsHelp
            from TestView tv
            ) tv
            group by Year_CW
            order by Year_CW desc;





            share|improve this answer














            You seems want conditional aggregation :



            select Year_CW,
            sum(case when col = 1 then 1 else 0 end) as one_count,
            sum(case when col = 0 then 1 else 0 end) as zero_count
            from (select Year_CW, IsStarted as col
            from TestView tv
            union all
            select Year_CW, NeedsHelp
            from TestView tv
            ) tv
            group by Year_CW
            order by Year_CW desc;






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 '18 at 13:31

























            answered Nov 21 '18 at 13:21









            Yogesh Sharma

            28.3k51335




            28.3k51335












            • This does the work. Is there a way to sort the group "Year_CW" descending?
              – Odatas
              Nov 21 '18 at 13:31










            • @Odatas. . . You can do order by Year_CW desc.
              – Yogesh Sharma
              Nov 21 '18 at 13:32


















            • This does the work. Is there a way to sort the group "Year_CW" descending?
              – Odatas
              Nov 21 '18 at 13:31










            • @Odatas. . . You can do order by Year_CW desc.
              – Yogesh Sharma
              Nov 21 '18 at 13:32
















            This does the work. Is there a way to sort the group "Year_CW" descending?
            – Odatas
            Nov 21 '18 at 13:31




            This does the work. Is there a way to sort the group "Year_CW" descending?
            – Odatas
            Nov 21 '18 at 13:31












            @Odatas. . . You can do order by Year_CW desc.
            – Yogesh Sharma
            Nov 21 '18 at 13:32




            @Odatas. . . You can do order by Year_CW desc.
            – Yogesh Sharma
            Nov 21 '18 at 13:32













            1














            select Year_CW
            , sum(case when Is_Started = 1 then 1 end) as Is_Started_1
            , sum(case when Is_Started = 0 then 1 end) as Is_Started_0
            , sum(case when Needs_Help = 1 then 1 end) as Needs_Help_1
            , sum(case when Needs_Help = 0 then 1 end) as Needs_Help_0
            from Test_View
            group by Year_CW


            So how I did it is I created 4 new fields for you. First one is giving value ‘1’ to every field where ‘Is_Started = 1’ and then I sum the instances. I did the same for 0 values and another two fields for values 1 and 0 for ‘Needs_ Help’ column. I believe this will give you your desired result.






            share|improve this answer























            • Adding an explanation of the query provided would be good to help others understand what you have done.
              – Dessus
              Nov 21 '18 at 21:02










            • Comment added brother. Sorry.
              – Filip Kubiak
              Nov 21 '18 at 21:09
















            1














            select Year_CW
            , sum(case when Is_Started = 1 then 1 end) as Is_Started_1
            , sum(case when Is_Started = 0 then 1 end) as Is_Started_0
            , sum(case when Needs_Help = 1 then 1 end) as Needs_Help_1
            , sum(case when Needs_Help = 0 then 1 end) as Needs_Help_0
            from Test_View
            group by Year_CW


            So how I did it is I created 4 new fields for you. First one is giving value ‘1’ to every field where ‘Is_Started = 1’ and then I sum the instances. I did the same for 0 values and another two fields for values 1 and 0 for ‘Needs_ Help’ column. I believe this will give you your desired result.






            share|improve this answer























            • Adding an explanation of the query provided would be good to help others understand what you have done.
              – Dessus
              Nov 21 '18 at 21:02










            • Comment added brother. Sorry.
              – Filip Kubiak
              Nov 21 '18 at 21:09














            1












            1








            1






            select Year_CW
            , sum(case when Is_Started = 1 then 1 end) as Is_Started_1
            , sum(case when Is_Started = 0 then 1 end) as Is_Started_0
            , sum(case when Needs_Help = 1 then 1 end) as Needs_Help_1
            , sum(case when Needs_Help = 0 then 1 end) as Needs_Help_0
            from Test_View
            group by Year_CW


            So how I did it is I created 4 new fields for you. First one is giving value ‘1’ to every field where ‘Is_Started = 1’ and then I sum the instances. I did the same for 0 values and another two fields for values 1 and 0 for ‘Needs_ Help’ column. I believe this will give you your desired result.






            share|improve this answer














            select Year_CW
            , sum(case when Is_Started = 1 then 1 end) as Is_Started_1
            , sum(case when Is_Started = 0 then 1 end) as Is_Started_0
            , sum(case when Needs_Help = 1 then 1 end) as Needs_Help_1
            , sum(case when Needs_Help = 0 then 1 end) as Needs_Help_0
            from Test_View
            group by Year_CW


            So how I did it is I created 4 new fields for you. First one is giving value ‘1’ to every field where ‘Is_Started = 1’ and then I sum the instances. I did the same for 0 values and another two fields for values 1 and 0 for ‘Needs_ Help’ column. I believe this will give you your desired result.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 '18 at 21:07

























            answered Nov 21 '18 at 20:59









            Filip Kubiak

            112




            112












            • Adding an explanation of the query provided would be good to help others understand what you have done.
              – Dessus
              Nov 21 '18 at 21:02










            • Comment added brother. Sorry.
              – Filip Kubiak
              Nov 21 '18 at 21:09


















            • Adding an explanation of the query provided would be good to help others understand what you have done.
              – Dessus
              Nov 21 '18 at 21:02










            • Comment added brother. Sorry.
              – Filip Kubiak
              Nov 21 '18 at 21:09
















            Adding an explanation of the query provided would be good to help others understand what you have done.
            – Dessus
            Nov 21 '18 at 21:02




            Adding an explanation of the query provided would be good to help others understand what you have done.
            – Dessus
            Nov 21 '18 at 21:02












            Comment added brother. Sorry.
            – Filip Kubiak
            Nov 21 '18 at 21:09




            Comment added brother. Sorry.
            – Filip Kubiak
            Nov 21 '18 at 21:09











            0














            So, if I'm understanding the question correctly you're just looking for the SUM of the additional two columns GROUP BY the Year_CW field. That would be the following.



            SELECT Year_CW, SUM([Is Started]), SUM([Needs Help])
            FROM TestView
            GROUP BY Year_CW





            share|improve this answer





















            • Not just the sum (This would only work for 1s) but the number of times specific entrys appear. But the "Group by" is the thing i was searching for.
              – Odatas
              Nov 21 '18 at 13:32
















            0














            So, if I'm understanding the question correctly you're just looking for the SUM of the additional two columns GROUP BY the Year_CW field. That would be the following.



            SELECT Year_CW, SUM([Is Started]), SUM([Needs Help])
            FROM TestView
            GROUP BY Year_CW





            share|improve this answer





















            • Not just the sum (This would only work for 1s) but the number of times specific entrys appear. But the "Group by" is the thing i was searching for.
              – Odatas
              Nov 21 '18 at 13:32














            0












            0








            0






            So, if I'm understanding the question correctly you're just looking for the SUM of the additional two columns GROUP BY the Year_CW field. That would be the following.



            SELECT Year_CW, SUM([Is Started]), SUM([Needs Help])
            FROM TestView
            GROUP BY Year_CW





            share|improve this answer












            So, if I'm understanding the question correctly you're just looking for the SUM of the additional two columns GROUP BY the Year_CW field. That would be the following.



            SELECT Year_CW, SUM([Is Started]), SUM([Needs Help])
            FROM TestView
            GROUP BY Year_CW






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 '18 at 13:20









            jradich1234

            1,11141725




            1,11141725












            • Not just the sum (This would only work for 1s) but the number of times specific entrys appear. But the "Group by" is the thing i was searching for.
              – Odatas
              Nov 21 '18 at 13:32


















            • Not just the sum (This would only work for 1s) but the number of times specific entrys appear. But the "Group by" is the thing i was searching for.
              – Odatas
              Nov 21 '18 at 13:32
















            Not just the sum (This would only work for 1s) but the number of times specific entrys appear. But the "Group by" is the thing i was searching for.
            – Odatas
            Nov 21 '18 at 13:32




            Not just the sum (This would only work for 1s) but the number of times specific entrys appear. But the "Group by" is the thing i was searching for.
            – Odatas
            Nov 21 '18 at 13:32











            0














            If it's only 0 or 1, then both 0 or 1 can be summed up.



            SELECT YEAR_CW, 
            SUM("Is Started") AS TotalStarted,
            SUM(1 - "Is Started") AS TotalNotStarted,
            SUM("Needs Help") AS TotalNeedsHelp,
            SUM(1 - "Needs Help") AS TotalNoHelpNeeded
            FROM TestView
            GROUP BY YEAR_CW
            ORDER BY YEAR_CW DESC





            share|improve this answer




























              0














              If it's only 0 or 1, then both 0 or 1 can be summed up.



              SELECT YEAR_CW, 
              SUM("Is Started") AS TotalStarted,
              SUM(1 - "Is Started") AS TotalNotStarted,
              SUM("Needs Help") AS TotalNeedsHelp,
              SUM(1 - "Needs Help") AS TotalNoHelpNeeded
              FROM TestView
              GROUP BY YEAR_CW
              ORDER BY YEAR_CW DESC





              share|improve this answer


























                0












                0








                0






                If it's only 0 or 1, then both 0 or 1 can be summed up.



                SELECT YEAR_CW, 
                SUM("Is Started") AS TotalStarted,
                SUM(1 - "Is Started") AS TotalNotStarted,
                SUM("Needs Help") AS TotalNeedsHelp,
                SUM(1 - "Needs Help") AS TotalNoHelpNeeded
                FROM TestView
                GROUP BY YEAR_CW
                ORDER BY YEAR_CW DESC





                share|improve this answer














                If it's only 0 or 1, then both 0 or 1 can be summed up.



                SELECT YEAR_CW, 
                SUM("Is Started") AS TotalStarted,
                SUM(1 - "Is Started") AS TotalNotStarted,
                SUM("Needs Help") AS TotalNeedsHelp,
                SUM(1 - "Needs Help") AS TotalNoHelpNeeded
                FROM TestView
                GROUP BY YEAR_CW
                ORDER BY YEAR_CW DESC






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 21 '18 at 13:40

























                answered Nov 21 '18 at 13:23









                LukStorms

                11.7k31532




                11.7k31532






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f53412834%2fcount-rows-with-specific-entry-for-every-distinct-entry-of-another-row%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

                    Wiesbaden

                    Marschland

                    Dieringhausen