Solved

mysql count

Posted on 2015-02-03
3
135 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now