join mysql select queries as 1 then group by
Using information of this site I have been able to do the join but im having the issues doing the group by
is there a way to get the below statements to run as 1
Query1
SELECT count(location),date
from `filter`
where location != "red"
group by date
Query2
SELECT count(location),date
from `filter`
where location = "red"
group by date
I did try the below but it outputs the wrong data
Query3
SELECT
date,
(select count(location) from `filter` where location != "red") AS indoor,
(select count(location) from `filter` where location = "red") AS outdoor
from `filter` group by date;
SQL Fiddle for each query
http://sqlfiddle.com/#!9/17ebea/4 (query1)
http://sqlfiddle.com/#!9/17ebea/6 (query2)
http://sqlfiddle.com/#!9/90c945/1 (query3)
mysql sql
add a comment |
Using information of this site I have been able to do the join but im having the issues doing the group by
is there a way to get the below statements to run as 1
Query1
SELECT count(location),date
from `filter`
where location != "red"
group by date
Query2
SELECT count(location),date
from `filter`
where location = "red"
group by date
I did try the below but it outputs the wrong data
Query3
SELECT
date,
(select count(location) from `filter` where location != "red") AS indoor,
(select count(location) from `filter` where location = "red") AS outdoor
from `filter` group by date;
SQL Fiddle for each query
http://sqlfiddle.com/#!9/17ebea/4 (query1)
http://sqlfiddle.com/#!9/17ebea/6 (query2)
http://sqlfiddle.com/#!9/90c945/1 (query3)
mysql sql
Well done! Pretty well formulated question, considering you are new :)
– Madhur Bhaiya
Nov 26 '18 at 11:12
thank you I took time to read the other question and tips people had given them on how to write question for clearer understanding
– a.parkes
Nov 26 '18 at 11:14
add a comment |
Using information of this site I have been able to do the join but im having the issues doing the group by
is there a way to get the below statements to run as 1
Query1
SELECT count(location),date
from `filter`
where location != "red"
group by date
Query2
SELECT count(location),date
from `filter`
where location = "red"
group by date
I did try the below but it outputs the wrong data
Query3
SELECT
date,
(select count(location) from `filter` where location != "red") AS indoor,
(select count(location) from `filter` where location = "red") AS outdoor
from `filter` group by date;
SQL Fiddle for each query
http://sqlfiddle.com/#!9/17ebea/4 (query1)
http://sqlfiddle.com/#!9/17ebea/6 (query2)
http://sqlfiddle.com/#!9/90c945/1 (query3)
mysql sql
Using information of this site I have been able to do the join but im having the issues doing the group by
is there a way to get the below statements to run as 1
Query1
SELECT count(location),date
from `filter`
where location != "red"
group by date
Query2
SELECT count(location),date
from `filter`
where location = "red"
group by date
I did try the below but it outputs the wrong data
Query3
SELECT
date,
(select count(location) from `filter` where location != "red") AS indoor,
(select count(location) from `filter` where location = "red") AS outdoor
from `filter` group by date;
SQL Fiddle for each query
http://sqlfiddle.com/#!9/17ebea/4 (query1)
http://sqlfiddle.com/#!9/17ebea/6 (query2)
http://sqlfiddle.com/#!9/90c945/1 (query3)
mysql sql
mysql sql
edited Nov 26 '18 at 11:22
Madhur Bhaiya
19.6k62336
19.6k62336
asked Nov 26 '18 at 11:10
a.parkesa.parkes
165
165
Well done! Pretty well formulated question, considering you are new :)
– Madhur Bhaiya
Nov 26 '18 at 11:12
thank you I took time to read the other question and tips people had given them on how to write question for clearer understanding
– a.parkes
Nov 26 '18 at 11:14
add a comment |
Well done! Pretty well formulated question, considering you are new :)
– Madhur Bhaiya
Nov 26 '18 at 11:12
thank you I took time to read the other question and tips people had given them on how to write question for clearer understanding
– a.parkes
Nov 26 '18 at 11:14
Well done! Pretty well formulated question, considering you are new :)
– Madhur Bhaiya
Nov 26 '18 at 11:12
Well done! Pretty well formulated question, considering you are new :)
– Madhur Bhaiya
Nov 26 '18 at 11:12
thank you I took time to read the other question and tips people had given them on how to write question for clearer understanding
– a.parkes
Nov 26 '18 at 11:14
thank you I took time to read the other question and tips people had given them on how to write question for clearer understanding
– a.parkes
Nov 26 '18 at 11:14
add a comment |
3 Answers
3
active
oldest
votes
SELECT
date,
COUNT(CASE WHEN location <> 'red' THEN location ELSE NULL END) AS indoor,
COUNT(CASE WHEN location = 'red' THEN location ELSE NULL END) AS outdoor
FROM filter
GROUP BY date;
https://dev.mysql.com/doc/refman/8.0/en/control-flow-functions.html#operator_case
thank you works exactly as i'd hoped :)
– a.parkes
Nov 26 '18 at 11:26
add a comment |
You can do conditional aggregation using CASE.. WHEN
expressions.
SELECT
date,
COUNT(CASE WHEN location = 'red' THEN location END) AS outdoor,
COUNT(CASE WHEN location <> 'red' THEN location END) AS indoor
FROM `filter`
GROUP BY date;
View on DB Fiddle
Result
| date | outdoor | indoor |
| ---------- | ------- | ------ |
| 2018-11-14 | 1 | 4 |
| 2018-11-15 | 1 | 0 |
| 2018-11-16 | 0 | 3 |
| 2018-11-17 | 1 | 1 |
| 2018-11-18 | 0 | 1 |
| 2018-11-19 | 0 | 2 |
| 2018-11-20 | 0 | 1 |
You can also use the following other variants, like using COUNT(1)
instead, or using SUM(..)
function.
Alternative #1
SELECT
date,
COUNT(CASE WHEN location = 'red' THEN 1 END) AS outdoor,
COUNT(CASE WHEN location <> 'red' THEN 1 END) AS indoor
FROM `filter`
GROUP BY date;
Alternative #2
SELECT
date,
SUM(CASE WHEN location = 'red' THEN 1 ELSE 0 END) AS outdoor,
SUM(CASE WHEN location <> 'red' THEN 1 ELSE 0 END) AS indoor
FROM `filter`
GROUP BY date;
add a comment |
In MySQL, I would use the shortcut that allows you to sum()
boolean variables:
select date, sum(location = 'red') as red,
sum(location <> 'red') as not_red
from filter
group by date ;
Notes:
- Use single quotes for string and date constants -- not double quotes. Single quotes are the standard delimiter.
<>
is the SQL inequality operator, although!=
is also supported by most databases.- This does not count
NULL
values.
To handle NULL
values, you might want:
sum(not location <=> 'red') as not_red
add a comment |
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%2f53479853%2fjoin-mysql-select-queries-as-1-then-group-by%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
SELECT
date,
COUNT(CASE WHEN location <> 'red' THEN location ELSE NULL END) AS indoor,
COUNT(CASE WHEN location = 'red' THEN location ELSE NULL END) AS outdoor
FROM filter
GROUP BY date;
https://dev.mysql.com/doc/refman/8.0/en/control-flow-functions.html#operator_case
thank you works exactly as i'd hoped :)
– a.parkes
Nov 26 '18 at 11:26
add a comment |
SELECT
date,
COUNT(CASE WHEN location <> 'red' THEN location ELSE NULL END) AS indoor,
COUNT(CASE WHEN location = 'red' THEN location ELSE NULL END) AS outdoor
FROM filter
GROUP BY date;
https://dev.mysql.com/doc/refman/8.0/en/control-flow-functions.html#operator_case
thank you works exactly as i'd hoped :)
– a.parkes
Nov 26 '18 at 11:26
add a comment |
SELECT
date,
COUNT(CASE WHEN location <> 'red' THEN location ELSE NULL END) AS indoor,
COUNT(CASE WHEN location = 'red' THEN location ELSE NULL END) AS outdoor
FROM filter
GROUP BY date;
https://dev.mysql.com/doc/refman/8.0/en/control-flow-functions.html#operator_case
SELECT
date,
COUNT(CASE WHEN location <> 'red' THEN location ELSE NULL END) AS indoor,
COUNT(CASE WHEN location = 'red' THEN location ELSE NULL END) AS outdoor
FROM filter
GROUP BY date;
https://dev.mysql.com/doc/refman/8.0/en/control-flow-functions.html#operator_case
answered Nov 26 '18 at 11:20
Siddharth NayarSiddharth Nayar
28915
28915
thank you works exactly as i'd hoped :)
– a.parkes
Nov 26 '18 at 11:26
add a comment |
thank you works exactly as i'd hoped :)
– a.parkes
Nov 26 '18 at 11:26
thank you works exactly as i'd hoped :)
– a.parkes
Nov 26 '18 at 11:26
thank you works exactly as i'd hoped :)
– a.parkes
Nov 26 '18 at 11:26
add a comment |
You can do conditional aggregation using CASE.. WHEN
expressions.
SELECT
date,
COUNT(CASE WHEN location = 'red' THEN location END) AS outdoor,
COUNT(CASE WHEN location <> 'red' THEN location END) AS indoor
FROM `filter`
GROUP BY date;
View on DB Fiddle
Result
| date | outdoor | indoor |
| ---------- | ------- | ------ |
| 2018-11-14 | 1 | 4 |
| 2018-11-15 | 1 | 0 |
| 2018-11-16 | 0 | 3 |
| 2018-11-17 | 1 | 1 |
| 2018-11-18 | 0 | 1 |
| 2018-11-19 | 0 | 2 |
| 2018-11-20 | 0 | 1 |
You can also use the following other variants, like using COUNT(1)
instead, or using SUM(..)
function.
Alternative #1
SELECT
date,
COUNT(CASE WHEN location = 'red' THEN 1 END) AS outdoor,
COUNT(CASE WHEN location <> 'red' THEN 1 END) AS indoor
FROM `filter`
GROUP BY date;
Alternative #2
SELECT
date,
SUM(CASE WHEN location = 'red' THEN 1 ELSE 0 END) AS outdoor,
SUM(CASE WHEN location <> 'red' THEN 1 ELSE 0 END) AS indoor
FROM `filter`
GROUP BY date;
add a comment |
You can do conditional aggregation using CASE.. WHEN
expressions.
SELECT
date,
COUNT(CASE WHEN location = 'red' THEN location END) AS outdoor,
COUNT(CASE WHEN location <> 'red' THEN location END) AS indoor
FROM `filter`
GROUP BY date;
View on DB Fiddle
Result
| date | outdoor | indoor |
| ---------- | ------- | ------ |
| 2018-11-14 | 1 | 4 |
| 2018-11-15 | 1 | 0 |
| 2018-11-16 | 0 | 3 |
| 2018-11-17 | 1 | 1 |
| 2018-11-18 | 0 | 1 |
| 2018-11-19 | 0 | 2 |
| 2018-11-20 | 0 | 1 |
You can also use the following other variants, like using COUNT(1)
instead, or using SUM(..)
function.
Alternative #1
SELECT
date,
COUNT(CASE WHEN location = 'red' THEN 1 END) AS outdoor,
COUNT(CASE WHEN location <> 'red' THEN 1 END) AS indoor
FROM `filter`
GROUP BY date;
Alternative #2
SELECT
date,
SUM(CASE WHEN location = 'red' THEN 1 ELSE 0 END) AS outdoor,
SUM(CASE WHEN location <> 'red' THEN 1 ELSE 0 END) AS indoor
FROM `filter`
GROUP BY date;
add a comment |
You can do conditional aggregation using CASE.. WHEN
expressions.
SELECT
date,
COUNT(CASE WHEN location = 'red' THEN location END) AS outdoor,
COUNT(CASE WHEN location <> 'red' THEN location END) AS indoor
FROM `filter`
GROUP BY date;
View on DB Fiddle
Result
| date | outdoor | indoor |
| ---------- | ------- | ------ |
| 2018-11-14 | 1 | 4 |
| 2018-11-15 | 1 | 0 |
| 2018-11-16 | 0 | 3 |
| 2018-11-17 | 1 | 1 |
| 2018-11-18 | 0 | 1 |
| 2018-11-19 | 0 | 2 |
| 2018-11-20 | 0 | 1 |
You can also use the following other variants, like using COUNT(1)
instead, or using SUM(..)
function.
Alternative #1
SELECT
date,
COUNT(CASE WHEN location = 'red' THEN 1 END) AS outdoor,
COUNT(CASE WHEN location <> 'red' THEN 1 END) AS indoor
FROM `filter`
GROUP BY date;
Alternative #2
SELECT
date,
SUM(CASE WHEN location = 'red' THEN 1 ELSE 0 END) AS outdoor,
SUM(CASE WHEN location <> 'red' THEN 1 ELSE 0 END) AS indoor
FROM `filter`
GROUP BY date;
You can do conditional aggregation using CASE.. WHEN
expressions.
SELECT
date,
COUNT(CASE WHEN location = 'red' THEN location END) AS outdoor,
COUNT(CASE WHEN location <> 'red' THEN location END) AS indoor
FROM `filter`
GROUP BY date;
View on DB Fiddle
Result
| date | outdoor | indoor |
| ---------- | ------- | ------ |
| 2018-11-14 | 1 | 4 |
| 2018-11-15 | 1 | 0 |
| 2018-11-16 | 0 | 3 |
| 2018-11-17 | 1 | 1 |
| 2018-11-18 | 0 | 1 |
| 2018-11-19 | 0 | 2 |
| 2018-11-20 | 0 | 1 |
You can also use the following other variants, like using COUNT(1)
instead, or using SUM(..)
function.
Alternative #1
SELECT
date,
COUNT(CASE WHEN location = 'red' THEN 1 END) AS outdoor,
COUNT(CASE WHEN location <> 'red' THEN 1 END) AS indoor
FROM `filter`
GROUP BY date;
Alternative #2
SELECT
date,
SUM(CASE WHEN location = 'red' THEN 1 ELSE 0 END) AS outdoor,
SUM(CASE WHEN location <> 'red' THEN 1 ELSE 0 END) AS indoor
FROM `filter`
GROUP BY date;
edited Nov 26 '18 at 11:20
answered Nov 26 '18 at 11:14
Madhur BhaiyaMadhur Bhaiya
19.6k62336
19.6k62336
add a comment |
add a comment |
In MySQL, I would use the shortcut that allows you to sum()
boolean variables:
select date, sum(location = 'red') as red,
sum(location <> 'red') as not_red
from filter
group by date ;
Notes:
- Use single quotes for string and date constants -- not double quotes. Single quotes are the standard delimiter.
<>
is the SQL inequality operator, although!=
is also supported by most databases.- This does not count
NULL
values.
To handle NULL
values, you might want:
sum(not location <=> 'red') as not_red
add a comment |
In MySQL, I would use the shortcut that allows you to sum()
boolean variables:
select date, sum(location = 'red') as red,
sum(location <> 'red') as not_red
from filter
group by date ;
Notes:
- Use single quotes for string and date constants -- not double quotes. Single quotes are the standard delimiter.
<>
is the SQL inequality operator, although!=
is also supported by most databases.- This does not count
NULL
values.
To handle NULL
values, you might want:
sum(not location <=> 'red') as not_red
add a comment |
In MySQL, I would use the shortcut that allows you to sum()
boolean variables:
select date, sum(location = 'red') as red,
sum(location <> 'red') as not_red
from filter
group by date ;
Notes:
- Use single quotes for string and date constants -- not double quotes. Single quotes are the standard delimiter.
<>
is the SQL inequality operator, although!=
is also supported by most databases.- This does not count
NULL
values.
To handle NULL
values, you might want:
sum(not location <=> 'red') as not_red
In MySQL, I would use the shortcut that allows you to sum()
boolean variables:
select date, sum(location = 'red') as red,
sum(location <> 'red') as not_red
from filter
group by date ;
Notes:
- Use single quotes for string and date constants -- not double quotes. Single quotes are the standard delimiter.
<>
is the SQL inequality operator, although!=
is also supported by most databases.- This does not count
NULL
values.
To handle NULL
values, you might want:
sum(not location <=> 'red') as not_red
answered Nov 26 '18 at 11:37
Gordon LinoffGordon Linoff
793k36316419
793k36316419
add a comment |
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.
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%2f53479853%2fjoin-mysql-select-queries-as-1-then-group-by%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
Well done! Pretty well formulated question, considering you are new :)
– Madhur Bhaiya
Nov 26 '18 at 11:12
thank you I took time to read the other question and tips people had given them on how to write question for clearer understanding
– a.parkes
Nov 26 '18 at 11:14