select 性别,
case when 年龄 between 20 and 29 then 1 else 0 end [20-29],
case when 年龄 between 30 and 39 then 1 else 0 end [30-39],
case when 年龄 between 40 and 49 then 1 else 0 end [40-49]
from 表名
group by 性别
以上使用于大部分数据库
只是在起别名上,只适用于sqlserver
oracle的话起别名
select 性别,
case when 年龄 between 20 and 29 then 1 else 0 end "20-29",
case when 年龄 between 30 and 39 then 1 else 0 end "30-39",
case when 年龄 between 40 and 49 then 1 else 0 end "40-49"
from 表名
group by 性别
mysql的话
select 性别,
case when 年龄 between 20 and 29 then 1 else 0 end `20-29,
case when 年龄 between 30 and 39 then 1 else 0 end `30-39`,
case when 年龄 between 40 and 49 then 1 else 0 end `40-49
from 表名
group by 性别