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
Solved

mysql count

Posted on 2015-02-03
3
138 Views
Last Modified: 2015-02-14
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
Comment
Question by:aej1973
  • 2
3 Comments
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 40587501
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
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 500 total points
ID: 40587502
That technique works reasonably well when there's a fixed (preferably small) number of statuses to be counted.
0
 

Author Closing Comment

by:aej1973
ID: 40610003
Thank you Daniel, this solution worked for me.

A
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

808 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