SELECT
date_format(entrydate,'%Y-%m-%b') YearMonth,
gender,COUNT(1) GenderCount
FROM
patient_master
GROUP BY
date_format(entrydate,'%Y-%m-%b'),gender
;
これがサンプルデータです
mysql> CREATE TABLE patient_master
-> (
-> id int not null auto_increment,
-> gender varchar(10),
-> entrydate date,
-> primary key (id)
-> );
Query OK, 0 rows affected (0.06 sec)
mysql> INSERT INTO patient_master (gender,entrydate) VALUES
-> ('Male' ,'2012-07-02'),
-> ('Female','2012-05-10'),
-> ('Male' ,'2012-05-25'),
-> ('Female','2012-07-09'),
-> ('Male' ,'2012-07-10'),
-> ('Female','2012-07-10'),
-> ('Male' ,'2012-07-10');
Query OK, 7 rows affected (0.06 sec)
Records: 7 Duplicates: 0 Warnings: 0
これが出力です
mysql> SELECT
-> date_format(entrydate,'%Y-%m-%b') YearMonth,
-> gender,COUNT(1) GenderCount
-> FROM
-> patient_master
-> GROUP BY
-> date_format(entrydate,'%Y-%m-%b'),gender
-> ;
+-------------+--------+-------------+
| YearMonth | gender | GenderCount |
+-------------+--------+-------------+
| 2012-05-May | Female | 1 |
| 2012-05-May | Male | 1 |
| 2012-07-Jul | Female | 2 |
| 2012-07-Jul | Male | 3 |
+-------------+--------+-------------+
4 rows in set (0.02 sec)
mysql>