SQL get count of one column values and group by another The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceAdd a column with a default value to an existing table in SQL ServerHow do you set a default value for a MySQL Datetime column?How can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL?Count(*) vs Count(1) - SQL ServerUsing group by on multiple columnsFinding duplicate values in a SQL tableWhat are the options for storing hierarchical data in a relational database?Find all tables containing column with specified name - MS SQL Server'IF' in 'SELECT' statement - choose output value based on column valuesSQL select only rows with max value on a column
How to delete random line from file using Unix command?
Python - Fishing Simulator
Are spiders unable to hurt humans, especially very small spiders?
How can I protect witches in combat who wear limited clothing?
How do you keep chess fun when your opponent constantly beats you?
Is it ethical to upload a automatically generated paper to a non peer-reviewed site as part of a larger research?
How does this infinite series simplify to an integral?
Why can't devices on different VLANs, but on the same subnet, communicate?
Are my PIs rude or am I just being too sensitive?
How to pronounce 1ターン?
Is every episode of "Where are my Pants?" identical?
How to test the equality of two Pearson correlation coefficients computed from the same sample?
Can undead you have reanimated wait inside a portable hole?
Why does the Event Horizon Telescope (EHT) not include telescopes from Africa, Asia or Australia?
Can smartphones with the same camera sensor have different image quality?
Was credit for the black hole image misattributed?
Do warforged have souls?
Finding the path in a graph from A to B then back to A with a minimum of shared edges
system() function string length limit
Windows 10: How to Lock (not sleep) laptop on lid close?
Why did all the guest students take carriages to the Yule Ball?
How many people can fit inside Mordenkainen's Magnificent Mansion?
Match Roman Numerals
Change bounding box of math glyphs in LuaTeX
SQL get count of one column values and group by another
The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceAdd a column with a default value to an existing table in SQL ServerHow do you set a default value for a MySQL Datetime column?How can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL?Count(*) vs Count(1) - SQL ServerUsing group by on multiple columnsFinding duplicate values in a SQL tableWhat are the options for storing hierarchical data in a relational database?Find all tables containing column with specified name - MS SQL Server'IF' in 'SELECT' statement - choose output value based on column valuesSQL select only rows with max value on a column
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a table where I have 3 columns like so:
NAME ZONE_ID GROUP_ID
Mark 11 1
Mary 11 1
Mart 12 1
Mike 11 2
Kent 13 2
Now I want to count all the zone_id-s for a specific group. So the output for group 1 would be that there are 2 entries in zone 11 and 1 entry in zone 12 and for group 2 that there is 1 entry for zone 11 and one entry in zone 13.
It doesn't seem like it would be a very difficult query but I have hard time searching for it the right way and haven't found anything useful.
mysql sql
add a comment |
I have a table where I have 3 columns like so:
NAME ZONE_ID GROUP_ID
Mark 11 1
Mary 11 1
Mart 12 1
Mike 11 2
Kent 13 2
Now I want to count all the zone_id-s for a specific group. So the output for group 1 would be that there are 2 entries in zone 11 and 1 entry in zone 12 and for group 2 that there is 1 entry for zone 11 and one entry in zone 13.
It doesn't seem like it would be a very difficult query but I have hard time searching for it the right way and haven't found anything useful.
mysql sql
add a comment |
I have a table where I have 3 columns like so:
NAME ZONE_ID GROUP_ID
Mark 11 1
Mary 11 1
Mart 12 1
Mike 11 2
Kent 13 2
Now I want to count all the zone_id-s for a specific group. So the output for group 1 would be that there are 2 entries in zone 11 and 1 entry in zone 12 and for group 2 that there is 1 entry for zone 11 and one entry in zone 13.
It doesn't seem like it would be a very difficult query but I have hard time searching for it the right way and haven't found anything useful.
mysql sql
I have a table where I have 3 columns like so:
NAME ZONE_ID GROUP_ID
Mark 11 1
Mary 11 1
Mart 12 1
Mike 11 2
Kent 13 2
Now I want to count all the zone_id-s for a specific group. So the output for group 1 would be that there are 2 entries in zone 11 and 1 entry in zone 12 and for group 2 that there is 1 entry for zone 11 and one entry in zone 13.
It doesn't seem like it would be a very difficult query but I have hard time searching for it the right way and haven't found anything useful.
mysql sql
mysql sql
asked Mar 8 at 13:30
tillinips12tillinips12
527
527
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You need to group by group_id, zone_id
:
select group_id, zone_id, count(*) counter
from tablename
group by group_id, zone_id
This is exactly what I needed, thank you. I did not know you could group by 2 different columns.
– tillinips12
Mar 8 at 13:41
Yes you can: dev.mysql.com/doc/refman/8.0/en/group-by-modifiers.html
– forpas
Mar 8 at 13:42
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%2f55064254%2fsql-get-count-of-one-column-values-and-group-by-another%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
You need to group by group_id, zone_id
:
select group_id, zone_id, count(*) counter
from tablename
group by group_id, zone_id
This is exactly what I needed, thank you. I did not know you could group by 2 different columns.
– tillinips12
Mar 8 at 13:41
Yes you can: dev.mysql.com/doc/refman/8.0/en/group-by-modifiers.html
– forpas
Mar 8 at 13:42
add a comment |
You need to group by group_id, zone_id
:
select group_id, zone_id, count(*) counter
from tablename
group by group_id, zone_id
This is exactly what I needed, thank you. I did not know you could group by 2 different columns.
– tillinips12
Mar 8 at 13:41
Yes you can: dev.mysql.com/doc/refman/8.0/en/group-by-modifiers.html
– forpas
Mar 8 at 13:42
add a comment |
You need to group by group_id, zone_id
:
select group_id, zone_id, count(*) counter
from tablename
group by group_id, zone_id
You need to group by group_id, zone_id
:
select group_id, zone_id, count(*) counter
from tablename
group by group_id, zone_id
answered Mar 8 at 13:39
forpasforpas
20.6k4830
20.6k4830
This is exactly what I needed, thank you. I did not know you could group by 2 different columns.
– tillinips12
Mar 8 at 13:41
Yes you can: dev.mysql.com/doc/refman/8.0/en/group-by-modifiers.html
– forpas
Mar 8 at 13:42
add a comment |
This is exactly what I needed, thank you. I did not know you could group by 2 different columns.
– tillinips12
Mar 8 at 13:41
Yes you can: dev.mysql.com/doc/refman/8.0/en/group-by-modifiers.html
– forpas
Mar 8 at 13:42
This is exactly what I needed, thank you. I did not know you could group by 2 different columns.
– tillinips12
Mar 8 at 13:41
This is exactly what I needed, thank you. I did not know you could group by 2 different columns.
– tillinips12
Mar 8 at 13:41
Yes you can: dev.mysql.com/doc/refman/8.0/en/group-by-modifiers.html
– forpas
Mar 8 at 13:42
Yes you can: dev.mysql.com/doc/refman/8.0/en/group-by-modifiers.html
– forpas
Mar 8 at 13:42
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%2f55064254%2fsql-get-count-of-one-column-values-and-group-by-another%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