请教二次分组统计查询应该怎么写?
表 person_master_index 字段有 age,sex,name.................
现在要统计成 这种
年龄段 男人数 女人数 总数
0-9 岁
10-19岁
.
.
.
.
.
我这么写不行啊
select a.age_group ,
count(*) ,
( select count(*) from
(select case age between 0 and 9 ...... end age_group ,sex from person_master_index)b
where b.age_group = a.age_group and b.sex = '男' ) man,
( select count(*) from
(select case age between 0 and 9 ...... end age_group ,sex from person_master_index)d
where d.age_group = a.age_group and b.sex = '女' ) woman
from
(select case age between 0 and 9 ...... end age_group ,sex from person_master_index) a
group by age_group
我试过如果(select case age between 0 and 9 ...... end age_group ,sex from person_master_index)这段语句换成一张实际存在的表 而不是view的话就没问题
可能是oracle不支持view的分组查询了吧
以为我把(select case age between 0 and 9 ...... end age_group ,sex from person_master_index)创建为view 然后用这个view来写sql 同样不行 哎郁闷
------最佳解决方案--------------------
with t as(
select 3 age,'0' sex,'a' name from dual
union all
select 45,'1','b' from dual
union all
select 32,'0','c' from dual
union all
select 12,'0','d' from dual
union all
select 56,'1','e' from dual
union all
select 22,'1','f' from dual
)
select decode(trunc(age / 10),
0,
'0-9岁',
1,
'10-19岁',
2,
'20-29岁',
3,
'30-39岁',
4,
&nb