Solved

mysql count

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySQL - need to create temporary table. 4 74
mySql Syntax 7 44
MYSQL simple update statement 3 46
How do I show the total amount of a equipment used on a assignment? 2 33
All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

24 Experts available now in Live!

Get 1:1 Help Now