Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 143
  • Last Modified:

mysql count

Hi have a table as follows:

id    name       status
1      AA              1
2      AA             1
3      BB              1
4       AA             2
5       BB              2
6       BB              2


The output I am looking for is as follows, is this possible?

name    count_status 1        count_status 2
AA           2                                    1
BB           1                                     2

Thanks for the help.

A
0
aej1973
Asked:
aej1973
  • 2
1 Solution
 
Daniel WilsonCommented:
select name,
sum(case when status = 1 then 1 else 0 end) as count_status1,
sum(case when status = 2 then 1 else 0 end) as count_status2
From MyTable group by name
0
 
Daniel WilsonCommented:
That technique works reasonably well when there's a fixed (preferably small) number of statuses to be counted.
0
 
aej1973Author Commented:
Thank you Daniel, this solution worked for me.

A
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now