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;








0















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.










share|improve this question




























    0















    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.










    share|improve this question
























      0












      0








      0








      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.










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 8 at 13:30









      tillinips12tillinips12

      527




      527






















          1 Answer
          1






          active

          oldest

          votes


















          1














          You need to group by group_id, zone_id:



          select group_id, zone_id, count(*) counter
          from tablename
          group by group_id, zone_id





          share|improve this answer























          • 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











          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%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









          1














          You need to group by group_id, zone_id:



          select group_id, zone_id, count(*) counter
          from tablename
          group by group_id, zone_id





          share|improve this answer























          • 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















          1














          You need to group by group_id, zone_id:



          select group_id, zone_id, count(*) counter
          from tablename
          group by group_id, zone_id





          share|improve this answer























          • 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













          1












          1








          1







          You need to group by group_id, zone_id:



          select group_id, zone_id, count(*) counter
          from tablename
          group by group_id, zone_id





          share|improve this answer













          You need to group by group_id, zone_id:



          select group_id, zone_id, count(*) counter
          from tablename
          group by group_id, zone_id






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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

















          • 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



















          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.




          draft saved


          draft discarded














          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





















































          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

          Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

          Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

          Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved