For example, I have a table like this:
| id | name | register_date | gender |
|1 |John |2011-02-02 |man |
|2 |Jane |2011-02-02 |woman |
|3 |Jihn |2012-04-04 |man |
|4 |Jeni |2012-02-02 |woman |
|5 |Joni |2012-02-02 |woman |
How is the query to create this result from that table above?
| RegisterYear | man | woman |
|2011 | 1 | 1 |
|2012 | 1 | 2 |
Group by the year and sum each gender
select year(register_date) as registeryear,
sum(gender = 'man') as man,
sum(gender = 'woman') as woman
from your_table
group by registeryear
Here you go:
SELECT YEAR(register_date) as RegisterYear,
SUM(gender = 'man') as man,
SUM(gender = 'woman') as woman
from register
group by RegisterYear;
Output:
REGISTERYEAR MAN WOMAN
===================================
2011 1 1
2012 1 2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.