Selecting rows that are not defined










1














How to select rows which are not defined? Like row 2 have undefined day 3 and row 3 have undefined day 1. I want them to be 0 in result set.



+----+-----+-------+
| id | day | count |
+----+-----+-------+
| 1 | 1 | 262 |
| 1 | 2 | 685 |
| 1 | 3 | 984 |
| 2 | 1 | 692 |
| 2 | 2 | 962 |
| 3 | 2 | 355 |
| 3 | 3 | 741 |
+----+-----+-------+


EDIT:
I want select count from days 1, 2 and 3 (not whole table) and display 0 on undefined day.










share|improve this question




























    1














    How to select rows which are not defined? Like row 2 have undefined day 3 and row 3 have undefined day 1. I want them to be 0 in result set.



    +----+-----+-------+
    | id | day | count |
    +----+-----+-------+
    | 1 | 1 | 262 |
    | 1 | 2 | 685 |
    | 1 | 3 | 984 |
    | 2 | 1 | 692 |
    | 2 | 2 | 962 |
    | 3 | 2 | 355 |
    | 3 | 3 | 741 |
    +----+-----+-------+


    EDIT:
    I want select count from days 1, 2 and 3 (not whole table) and display 0 on undefined day.










    share|improve this question


























      1












      1








      1







      How to select rows which are not defined? Like row 2 have undefined day 3 and row 3 have undefined day 1. I want them to be 0 in result set.



      +----+-----+-------+
      | id | day | count |
      +----+-----+-------+
      | 1 | 1 | 262 |
      | 1 | 2 | 685 |
      | 1 | 3 | 984 |
      | 2 | 1 | 692 |
      | 2 | 2 | 962 |
      | 3 | 2 | 355 |
      | 3 | 3 | 741 |
      +----+-----+-------+


      EDIT:
      I want select count from days 1, 2 and 3 (not whole table) and display 0 on undefined day.










      share|improve this question















      How to select rows which are not defined? Like row 2 have undefined day 3 and row 3 have undefined day 1. I want them to be 0 in result set.



      +----+-----+-------+
      | id | day | count |
      +----+-----+-------+
      | 1 | 1 | 262 |
      | 1 | 2 | 685 |
      | 1 | 3 | 984 |
      | 2 | 1 | 692 |
      | 2 | 2 | 962 |
      | 3 | 2 | 355 |
      | 3 | 3 | 741 |
      +----+-----+-------+


      EDIT:
      I want select count from days 1, 2 and 3 (not whole table) and display 0 on undefined day.







      mysql mariadb






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 18:17

























      asked Nov 12 '18 at 18:04









      User161

      133




      133






















          1 Answer
          1






          active

          oldest

          votes


















          0














          • We can get all unique id values in a Derived Table.

          • For day, you seem to want only 1,2 and 3 only. So we can directly consider these values only using UNION ALL.


          • CROSS JOIN between them to get all possible combinations.


          • LEFT JOIN from all_combinations table to the main table on id and day.

          • We can use Coalesce() function to consider 0 value for count, for the cases where there is no matching row in the main table

          Try the following:



          SELECT all_combinations.id, 
          all_combinations.day,
          COALESCE(t.count, 0) AS count
          FROM
          (
          SELECT ids.id, days.day
          FROM
          (SELECT DISTINCT id FROM your_table) AS ids
          CROSS JOIN
          (SELECT 1 AS day UNION ALL SELECT 2 UNION ALL SELECT 3) AS days
          ) AS all_combinations
          LEFT JOIN your_table AS t
          ON t.id = all_combinations.id AND
          t.day = all_combinations.day


          Result:



          | id | day | count |
          | --- | --- | ----- |
          | 1 | 1 | 262 |
          | 2 | 1 | 692 |
          | 3 | 1 | 0 |
          | 1 | 2 | 685 |
          | 2 | 2 | 962 |
          | 3 | 2 | 355 |
          | 1 | 3 | 984 |
          | 2 | 3 | 0 |
          | 3 | 3 | 741 |



          View on DB Fiddle






          share|improve this answer






















          • This works nice for whole table.
            – User161
            Nov 12 '18 at 18:30










          • @User161 pls check the updated answer now.
            – Madhur Bhaiya
            Nov 12 '18 at 18:31










          • Selects undefined row, but all counts are 0.
            – User161
            Nov 12 '18 at 18:39










          • @User161 can you please set up a db-fiddle.com
            – Madhur Bhaiya
            Nov 12 '18 at 18:41






          • 1




            db-fiddle.com/f/v1WzMuLfgzbXC4Y8XB9bsT/0
            – User161
            Nov 12 '18 at 18:53










          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%2f53267722%2fselecting-rows-that-are-not-defined%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














          • We can get all unique id values in a Derived Table.

          • For day, you seem to want only 1,2 and 3 only. So we can directly consider these values only using UNION ALL.


          • CROSS JOIN between them to get all possible combinations.


          • LEFT JOIN from all_combinations table to the main table on id and day.

          • We can use Coalesce() function to consider 0 value for count, for the cases where there is no matching row in the main table

          Try the following:



          SELECT all_combinations.id, 
          all_combinations.day,
          COALESCE(t.count, 0) AS count
          FROM
          (
          SELECT ids.id, days.day
          FROM
          (SELECT DISTINCT id FROM your_table) AS ids
          CROSS JOIN
          (SELECT 1 AS day UNION ALL SELECT 2 UNION ALL SELECT 3) AS days
          ) AS all_combinations
          LEFT JOIN your_table AS t
          ON t.id = all_combinations.id AND
          t.day = all_combinations.day


          Result:



          | id | day | count |
          | --- | --- | ----- |
          | 1 | 1 | 262 |
          | 2 | 1 | 692 |
          | 3 | 1 | 0 |
          | 1 | 2 | 685 |
          | 2 | 2 | 962 |
          | 3 | 2 | 355 |
          | 1 | 3 | 984 |
          | 2 | 3 | 0 |
          | 3 | 3 | 741 |



          View on DB Fiddle






          share|improve this answer






















          • This works nice for whole table.
            – User161
            Nov 12 '18 at 18:30










          • @User161 pls check the updated answer now.
            – Madhur Bhaiya
            Nov 12 '18 at 18:31










          • Selects undefined row, but all counts are 0.
            – User161
            Nov 12 '18 at 18:39










          • @User161 can you please set up a db-fiddle.com
            – Madhur Bhaiya
            Nov 12 '18 at 18:41






          • 1




            db-fiddle.com/f/v1WzMuLfgzbXC4Y8XB9bsT/0
            – User161
            Nov 12 '18 at 18:53















          0














          • We can get all unique id values in a Derived Table.

          • For day, you seem to want only 1,2 and 3 only. So we can directly consider these values only using UNION ALL.


          • CROSS JOIN between them to get all possible combinations.


          • LEFT JOIN from all_combinations table to the main table on id and day.

          • We can use Coalesce() function to consider 0 value for count, for the cases where there is no matching row in the main table

          Try the following:



          SELECT all_combinations.id, 
          all_combinations.day,
          COALESCE(t.count, 0) AS count
          FROM
          (
          SELECT ids.id, days.day
          FROM
          (SELECT DISTINCT id FROM your_table) AS ids
          CROSS JOIN
          (SELECT 1 AS day UNION ALL SELECT 2 UNION ALL SELECT 3) AS days
          ) AS all_combinations
          LEFT JOIN your_table AS t
          ON t.id = all_combinations.id AND
          t.day = all_combinations.day


          Result:



          | id | day | count |
          | --- | --- | ----- |
          | 1 | 1 | 262 |
          | 2 | 1 | 692 |
          | 3 | 1 | 0 |
          | 1 | 2 | 685 |
          | 2 | 2 | 962 |
          | 3 | 2 | 355 |
          | 1 | 3 | 984 |
          | 2 | 3 | 0 |
          | 3 | 3 | 741 |



          View on DB Fiddle






          share|improve this answer






















          • This works nice for whole table.
            – User161
            Nov 12 '18 at 18:30










          • @User161 pls check the updated answer now.
            – Madhur Bhaiya
            Nov 12 '18 at 18:31










          • Selects undefined row, but all counts are 0.
            – User161
            Nov 12 '18 at 18:39










          • @User161 can you please set up a db-fiddle.com
            – Madhur Bhaiya
            Nov 12 '18 at 18:41






          • 1




            db-fiddle.com/f/v1WzMuLfgzbXC4Y8XB9bsT/0
            – User161
            Nov 12 '18 at 18:53













          0












          0








          0






          • We can get all unique id values in a Derived Table.

          • For day, you seem to want only 1,2 and 3 only. So we can directly consider these values only using UNION ALL.


          • CROSS JOIN between them to get all possible combinations.


          • LEFT JOIN from all_combinations table to the main table on id and day.

          • We can use Coalesce() function to consider 0 value for count, for the cases where there is no matching row in the main table

          Try the following:



          SELECT all_combinations.id, 
          all_combinations.day,
          COALESCE(t.count, 0) AS count
          FROM
          (
          SELECT ids.id, days.day
          FROM
          (SELECT DISTINCT id FROM your_table) AS ids
          CROSS JOIN
          (SELECT 1 AS day UNION ALL SELECT 2 UNION ALL SELECT 3) AS days
          ) AS all_combinations
          LEFT JOIN your_table AS t
          ON t.id = all_combinations.id AND
          t.day = all_combinations.day


          Result:



          | id | day | count |
          | --- | --- | ----- |
          | 1 | 1 | 262 |
          | 2 | 1 | 692 |
          | 3 | 1 | 0 |
          | 1 | 2 | 685 |
          | 2 | 2 | 962 |
          | 3 | 2 | 355 |
          | 1 | 3 | 984 |
          | 2 | 3 | 0 |
          | 3 | 3 | 741 |



          View on DB Fiddle






          share|improve this answer














          • We can get all unique id values in a Derived Table.

          • For day, you seem to want only 1,2 and 3 only. So we can directly consider these values only using UNION ALL.


          • CROSS JOIN between them to get all possible combinations.


          • LEFT JOIN from all_combinations table to the main table on id and day.

          • We can use Coalesce() function to consider 0 value for count, for the cases where there is no matching row in the main table

          Try the following:



          SELECT all_combinations.id, 
          all_combinations.day,
          COALESCE(t.count, 0) AS count
          FROM
          (
          SELECT ids.id, days.day
          FROM
          (SELECT DISTINCT id FROM your_table) AS ids
          CROSS JOIN
          (SELECT 1 AS day UNION ALL SELECT 2 UNION ALL SELECT 3) AS days
          ) AS all_combinations
          LEFT JOIN your_table AS t
          ON t.id = all_combinations.id AND
          t.day = all_combinations.day


          Result:



          | id | day | count |
          | --- | --- | ----- |
          | 1 | 1 | 262 |
          | 2 | 1 | 692 |
          | 3 | 1 | 0 |
          | 1 | 2 | 685 |
          | 2 | 2 | 962 |
          | 3 | 2 | 355 |
          | 1 | 3 | 984 |
          | 2 | 3 | 0 |
          | 3 | 3 | 741 |



          View on DB Fiddle







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 '18 at 19:06

























          answered Nov 12 '18 at 18:12









          Madhur Bhaiya

          19.5k62236




          19.5k62236











          • This works nice for whole table.
            – User161
            Nov 12 '18 at 18:30










          • @User161 pls check the updated answer now.
            – Madhur Bhaiya
            Nov 12 '18 at 18:31










          • Selects undefined row, but all counts are 0.
            – User161
            Nov 12 '18 at 18:39










          • @User161 can you please set up a db-fiddle.com
            – Madhur Bhaiya
            Nov 12 '18 at 18:41






          • 1




            db-fiddle.com/f/v1WzMuLfgzbXC4Y8XB9bsT/0
            – User161
            Nov 12 '18 at 18:53
















          • This works nice for whole table.
            – User161
            Nov 12 '18 at 18:30










          • @User161 pls check the updated answer now.
            – Madhur Bhaiya
            Nov 12 '18 at 18:31










          • Selects undefined row, but all counts are 0.
            – User161
            Nov 12 '18 at 18:39










          • @User161 can you please set up a db-fiddle.com
            – Madhur Bhaiya
            Nov 12 '18 at 18:41






          • 1




            db-fiddle.com/f/v1WzMuLfgzbXC4Y8XB9bsT/0
            – User161
            Nov 12 '18 at 18:53















          This works nice for whole table.
          – User161
          Nov 12 '18 at 18:30




          This works nice for whole table.
          – User161
          Nov 12 '18 at 18:30












          @User161 pls check the updated answer now.
          – Madhur Bhaiya
          Nov 12 '18 at 18:31




          @User161 pls check the updated answer now.
          – Madhur Bhaiya
          Nov 12 '18 at 18:31












          Selects undefined row, but all counts are 0.
          – User161
          Nov 12 '18 at 18:39




          Selects undefined row, but all counts are 0.
          – User161
          Nov 12 '18 at 18:39












          @User161 can you please set up a db-fiddle.com
          – Madhur Bhaiya
          Nov 12 '18 at 18:41




          @User161 can you please set up a db-fiddle.com
          – Madhur Bhaiya
          Nov 12 '18 at 18:41




          1




          1




          db-fiddle.com/f/v1WzMuLfgzbXC4Y8XB9bsT/0
          – User161
          Nov 12 '18 at 18:53




          db-fiddle.com/f/v1WzMuLfgzbXC4Y8XB9bsT/0
          – User161
          Nov 12 '18 at 18:53

















          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%2f53267722%2fselecting-rows-that-are-not-defined%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







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3