Selecting rows that are not defined
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
add a comment |
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
add a comment |
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
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
mysql mariadb
edited Nov 12 '18 at 18:17
asked Nov 12 '18 at 18:04
User161
133
133
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
- 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 usingUNION ALL
. CROSS JOIN
between them to get all possible combinations.LEFT JOIN
fromall_combinations
table to the main table onid
andday
.- 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
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
- 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 usingUNION ALL
. CROSS JOIN
between them to get all possible combinations.LEFT JOIN
fromall_combinations
table to the main table onid
andday
.- 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
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
add a comment |
- 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 usingUNION ALL
. CROSS JOIN
between them to get all possible combinations.LEFT JOIN
fromall_combinations
table to the main table onid
andday
.- 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
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
add a comment |
- 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 usingUNION ALL
. CROSS JOIN
between them to get all possible combinations.LEFT JOIN
fromall_combinations
table to the main table onid
andday
.- 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
- 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 usingUNION ALL
. CROSS JOIN
between them to get all possible combinations.LEFT JOIN
fromall_combinations
table to the main table onid
andday
.- 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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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