SQL - Why does my output from subqueries display my answer multiple times?












-4














The output to the following query is repeated for the number of rows in my table. Can anyone explain why and if using 'Limit 1' to solve this is a good habit to get into?



SELECT

CAST((SELECT COUNT(event_id)
FROM creation_funnel
WHERE Ds = 2018-10-01 AND action = "publish") AS float) /

CAST((SELECT COUNT(DISTINCT event_id)
FROM creation_funnel
WHERE Ds = 2018-10-01) AS float)
* 100.0

FROM creation_funnel;









share|improve this question
























  • I am not sure, but it looks strange to me, that you are using aggregation function Count without the GROUP BY statement.
    – Patrik Valkovič
    Nov 11 at 22:18










  • Try removing the FROM creation_funnel at the end
    – Mohammad C
    Nov 11 at 22:20








  • 3




    2018-10-01 = 2007
    – Strawberry
    Nov 11 at 22:40
















-4














The output to the following query is repeated for the number of rows in my table. Can anyone explain why and if using 'Limit 1' to solve this is a good habit to get into?



SELECT

CAST((SELECT COUNT(event_id)
FROM creation_funnel
WHERE Ds = 2018-10-01 AND action = "publish") AS float) /

CAST((SELECT COUNT(DISTINCT event_id)
FROM creation_funnel
WHERE Ds = 2018-10-01) AS float)
* 100.0

FROM creation_funnel;









share|improve this question
























  • I am not sure, but it looks strange to me, that you are using aggregation function Count without the GROUP BY statement.
    – Patrik Valkovič
    Nov 11 at 22:18










  • Try removing the FROM creation_funnel at the end
    – Mohammad C
    Nov 11 at 22:20








  • 3




    2018-10-01 = 2007
    – Strawberry
    Nov 11 at 22:40














-4












-4








-4







The output to the following query is repeated for the number of rows in my table. Can anyone explain why and if using 'Limit 1' to solve this is a good habit to get into?



SELECT

CAST((SELECT COUNT(event_id)
FROM creation_funnel
WHERE Ds = 2018-10-01 AND action = "publish") AS float) /

CAST((SELECT COUNT(DISTINCT event_id)
FROM creation_funnel
WHERE Ds = 2018-10-01) AS float)
* 100.0

FROM creation_funnel;









share|improve this question















The output to the following query is repeated for the number of rows in my table. Can anyone explain why and if using 'Limit 1' to solve this is a good habit to get into?



SELECT

CAST((SELECT COUNT(event_id)
FROM creation_funnel
WHERE Ds = 2018-10-01 AND action = "publish") AS float) /

CAST((SELECT COUNT(DISTINCT event_id)
FROM creation_funnel
WHERE Ds = 2018-10-01) AS float)
* 100.0

FROM creation_funnel;






mysql sql subquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 22:26









Strawberry

25.8k83149




25.8k83149










asked Nov 11 at 22:15









user2229066

80312




80312












  • I am not sure, but it looks strange to me, that you are using aggregation function Count without the GROUP BY statement.
    – Patrik Valkovič
    Nov 11 at 22:18










  • Try removing the FROM creation_funnel at the end
    – Mohammad C
    Nov 11 at 22:20








  • 3




    2018-10-01 = 2007
    – Strawberry
    Nov 11 at 22:40


















  • I am not sure, but it looks strange to me, that you are using aggregation function Count without the GROUP BY statement.
    – Patrik Valkovič
    Nov 11 at 22:18










  • Try removing the FROM creation_funnel at the end
    – Mohammad C
    Nov 11 at 22:20








  • 3




    2018-10-01 = 2007
    – Strawberry
    Nov 11 at 22:40
















I am not sure, but it looks strange to me, that you are using aggregation function Count without the GROUP BY statement.
– Patrik Valkovič
Nov 11 at 22:18




I am not sure, but it looks strange to me, that you are using aggregation function Count without the GROUP BY statement.
– Patrik Valkovič
Nov 11 at 22:18












Try removing the FROM creation_funnel at the end
– Mohammad C
Nov 11 at 22:20






Try removing the FROM creation_funnel at the end
– Mohammad C
Nov 11 at 22:20






3




3




2018-10-01 = 2007
– Strawberry
Nov 11 at 22:40




2018-10-01 = 2007
– Strawberry
Nov 11 at 22:40












2 Answers
2






active

oldest

votes


















2














This query would be a good candidate for conditional aggregation as it will enable you to get rid of the two subqueries. Try this instead:



SELECT
COUNT(CASE WHEN Ds = '2018-10-01' AND action = 'publish' THEN event_id END) /
COUNT(DISTINCT CASE WHEN Ds = '2018-10-01' THEN event_id END) * 100 AS percent_published
FROM creation_funnel


Note that date strings need to be enclosed in quotes, as @Strawberry points out, 2018-10-01 is interpreted as an integer expression, evaluating to 2007.



This query will also only give you one result compared to your query, which will give you a result for every row in the table. Either removing the FROM clause, or adding LIMIT 1 would resolve that, but using conditional aggregation is a better alternative.






share|improve this answer





























    -2














    SELECT
    CAST((SELECT COUNT(event_id) FROM creation_funnel WHERE Ds = '2018-10-01' AND action = "publish") AS float) /
    CAST((SELECT COUNT(DISTINCT event_id) FROM creation_funnel WHERE Ds = '2018-10-01') AS float) * 100.0


    The repetition is because of the FROM part of the statement. Remove it.
    Your are getting multiple results because the select is being run for each record in the table.



    updated



    Based on what nick said. You could simplify his answer to the following.



    SELECT 
    COUNT(CASE WHEN action = 'publish' THEN event_id ELSE 0 END) /
    COUNT(DISTINCT event_id) * 100 AS percent_published
    FROM creation_funnel
    WHERE Ds = '2018-10-01';





    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%2f53253784%2fsql-why-does-my-output-from-subqueries-display-my-answer-multiple-times%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









      2














      This query would be a good candidate for conditional aggregation as it will enable you to get rid of the two subqueries. Try this instead:



      SELECT
      COUNT(CASE WHEN Ds = '2018-10-01' AND action = 'publish' THEN event_id END) /
      COUNT(DISTINCT CASE WHEN Ds = '2018-10-01' THEN event_id END) * 100 AS percent_published
      FROM creation_funnel


      Note that date strings need to be enclosed in quotes, as @Strawberry points out, 2018-10-01 is interpreted as an integer expression, evaluating to 2007.



      This query will also only give you one result compared to your query, which will give you a result for every row in the table. Either removing the FROM clause, or adding LIMIT 1 would resolve that, but using conditional aggregation is a better alternative.






      share|improve this answer


























        2














        This query would be a good candidate for conditional aggregation as it will enable you to get rid of the two subqueries. Try this instead:



        SELECT
        COUNT(CASE WHEN Ds = '2018-10-01' AND action = 'publish' THEN event_id END) /
        COUNT(DISTINCT CASE WHEN Ds = '2018-10-01' THEN event_id END) * 100 AS percent_published
        FROM creation_funnel


        Note that date strings need to be enclosed in quotes, as @Strawberry points out, 2018-10-01 is interpreted as an integer expression, evaluating to 2007.



        This query will also only give you one result compared to your query, which will give you a result for every row in the table. Either removing the FROM clause, or adding LIMIT 1 would resolve that, but using conditional aggregation is a better alternative.






        share|improve this answer
























          2












          2








          2






          This query would be a good candidate for conditional aggregation as it will enable you to get rid of the two subqueries. Try this instead:



          SELECT
          COUNT(CASE WHEN Ds = '2018-10-01' AND action = 'publish' THEN event_id END) /
          COUNT(DISTINCT CASE WHEN Ds = '2018-10-01' THEN event_id END) * 100 AS percent_published
          FROM creation_funnel


          Note that date strings need to be enclosed in quotes, as @Strawberry points out, 2018-10-01 is interpreted as an integer expression, evaluating to 2007.



          This query will also only give you one result compared to your query, which will give you a result for every row in the table. Either removing the FROM clause, or adding LIMIT 1 would resolve that, but using conditional aggregation is a better alternative.






          share|improve this answer












          This query would be a good candidate for conditional aggregation as it will enable you to get rid of the two subqueries. Try this instead:



          SELECT
          COUNT(CASE WHEN Ds = '2018-10-01' AND action = 'publish' THEN event_id END) /
          COUNT(DISTINCT CASE WHEN Ds = '2018-10-01' THEN event_id END) * 100 AS percent_published
          FROM creation_funnel


          Note that date strings need to be enclosed in quotes, as @Strawberry points out, 2018-10-01 is interpreted as an integer expression, evaluating to 2007.



          This query will also only give you one result compared to your query, which will give you a result for every row in the table. Either removing the FROM clause, or adding LIMIT 1 would resolve that, but using conditional aggregation is a better alternative.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 23:14









          Nick

          23.5k91535




          23.5k91535

























              -2














              SELECT
              CAST((SELECT COUNT(event_id) FROM creation_funnel WHERE Ds = '2018-10-01' AND action = "publish") AS float) /
              CAST((SELECT COUNT(DISTINCT event_id) FROM creation_funnel WHERE Ds = '2018-10-01') AS float) * 100.0


              The repetition is because of the FROM part of the statement. Remove it.
              Your are getting multiple results because the select is being run for each record in the table.



              updated



              Based on what nick said. You could simplify his answer to the following.



              SELECT 
              COUNT(CASE WHEN action = 'publish' THEN event_id ELSE 0 END) /
              COUNT(DISTINCT event_id) * 100 AS percent_published
              FROM creation_funnel
              WHERE Ds = '2018-10-01';





              share|improve this answer




























                -2














                SELECT
                CAST((SELECT COUNT(event_id) FROM creation_funnel WHERE Ds = '2018-10-01' AND action = "publish") AS float) /
                CAST((SELECT COUNT(DISTINCT event_id) FROM creation_funnel WHERE Ds = '2018-10-01') AS float) * 100.0


                The repetition is because of the FROM part of the statement. Remove it.
                Your are getting multiple results because the select is being run for each record in the table.



                updated



                Based on what nick said. You could simplify his answer to the following.



                SELECT 
                COUNT(CASE WHEN action = 'publish' THEN event_id ELSE 0 END) /
                COUNT(DISTINCT event_id) * 100 AS percent_published
                FROM creation_funnel
                WHERE Ds = '2018-10-01';





                share|improve this answer


























                  -2












                  -2








                  -2






                  SELECT
                  CAST((SELECT COUNT(event_id) FROM creation_funnel WHERE Ds = '2018-10-01' AND action = "publish") AS float) /
                  CAST((SELECT COUNT(DISTINCT event_id) FROM creation_funnel WHERE Ds = '2018-10-01') AS float) * 100.0


                  The repetition is because of the FROM part of the statement. Remove it.
                  Your are getting multiple results because the select is being run for each record in the table.



                  updated



                  Based on what nick said. You could simplify his answer to the following.



                  SELECT 
                  COUNT(CASE WHEN action = 'publish' THEN event_id ELSE 0 END) /
                  COUNT(DISTINCT event_id) * 100 AS percent_published
                  FROM creation_funnel
                  WHERE Ds = '2018-10-01';





                  share|improve this answer














                  SELECT
                  CAST((SELECT COUNT(event_id) FROM creation_funnel WHERE Ds = '2018-10-01' AND action = "publish") AS float) /
                  CAST((SELECT COUNT(DISTINCT event_id) FROM creation_funnel WHERE Ds = '2018-10-01') AS float) * 100.0


                  The repetition is because of the FROM part of the statement. Remove it.
                  Your are getting multiple results because the select is being run for each record in the table.



                  updated



                  Based on what nick said. You could simplify his answer to the following.



                  SELECT 
                  COUNT(CASE WHEN action = 'publish' THEN event_id ELSE 0 END) /
                  COUNT(DISTINCT event_id) * 100 AS percent_published
                  FROM creation_funnel
                  WHERE Ds = '2018-10-01';






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 12 at 19:12

























                  answered Nov 11 at 22:21









                  Mohammad C

                  1,1831312




                  1,1831312






























                      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%2f53253784%2fsql-why-does-my-output-from-subqueries-display-my-answer-multiple-times%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

                      Full-time equivalent

                      Bicuculline

                      さくらももこ