Skip navigation links

MySQL Forums :: General :: Count two different values from the same column


Advanced Search

Re: Count two different values from the same column
Posted by: laptop alias ()
Date: December 02, 2010 06:47AM

CREATE TABLE members
(member_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,name VARCHAR(12) NOT NULL
,gender CHAR(1) NOT NULL
);

INSERT INTO members VALUES 
(1,'John','m'),
(2,'Yoko','f'),
(3,'Paul','m'),
(4,'Linda','f'),
(5,'George','m'),
(6,'Ringo','m');

 SELECT * FROM members;
+-----------+--------+--------+
| member_id | name   | gender |
+-----------+--------+--------+
|         1 | John   | m      |
|         2 | Yoko   | f      |
|         3 | Paul   | m      |
|         4 | Linda  | f      |
|         5 | George | m      |
|         6 | Ringo  | m      |
+-----------+--------+--------+

SELECT gender
     , COUNT(*) total 
  FROM members 
 GROUP  
    BY gender;
+--------+-------+
| gender | total |
+--------+-------+
| f      |     2 |
| m      |     4 |
+--------+-------+

SELECT SUM(CASE WHEN gender = 'm' THEN 1 ELSE 0 END) m
     , SUM(CASE WHEN gender = 'f' THEN 1 ELSE 0 END) f
  FROM members;
+------+------+
| m    | f    |
+------+------+
|    4 |    2 |
+------+------+

Options: ReplyQuote


Subject Written By Posted
Count two different values from the same column Santino Puleio 12/02/2010 03:19AM
Re: Count two different values from the same column laptop alias 12/02/2010 06:47AM
Re: Count two different values from the same column Santino Puleio 12/02/2010 07:41AM
Re: Count two different values from the same column Bojan Tomic 12/03/2010 01:04PM
Re: Count two different values from the same column Chad Bourque 12/03/2010 01:44PM
Re: Count two different values from the same column laptop alias 12/03/2010 04:03PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.