?
Solved

mysql count and group by

Posted on 2014-12-08
4
Medium Priority
?
178 Views
Last Modified: 2014-12-12
I have a table as follows:

id       name
1        Complete
2        Incomplete
2        Incomplete
1        packaged
3        complete
4        packaged
4        packaged

How can I do a count statement to get the following output:
complete  2
incomplete 1
packaged 2

The reason that "incomplete" is 1 is because it is grouped by id, the same is with "packaged".  Can someone let me know how I can get a count of "name"  by grouping by 'id" ?

Thank you,
A
0
Comment
Question by:aej1973
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40488263
you have to "distinct" first:
select name , count(*)
  from ( select id, name from yourtable group by id, name )
group by name

Open in new window

0
 

Author Comment

by:aej1973
ID: 40490115
I am getting this error "Every derived table must have its own alias". What does this mean?
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 40490612
it means the subquery needs an alias:
select name , count(*)
  from ( select id, name from yourtable group by id, name ) SQ
group by name 

Open in new window

SQ is the alias name given to the subquery.
side note about "aliases" for columns and tables in general:
http://www.experts-exchange.com/Database/Miscellaneous/A_11135-Why-should-I-use-aliases-in-my-queries.html
0
 

Author Comment

by:aej1973
ID: 40496670
Thank you.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question