Solved

mysql count and group by

Posted on 2014-12-08
4
171 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 500 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

737 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