求一个分组查询稍微有点复杂
表aaa字段
a , b,
1 0
2 1
3 0
2 0
3 0
5 0
5 1
5 1
其中b,只有1 or 0 ,那我想查询按照 a来分组 统计 有多少条,然或再根据b来区分 各有多少,再算百分比;
期望结果如下
名字总笔数 0笔数 1笔数 1占百分比
1, 1 1 0 100%
2 2 1 1 50%
3 2 2 0 0%
5 3 1 2 66.7%
我希望一条语句搞定,当然也可以用其他方法。哈哈 一句的话估计够戗。水平不够,呵呵。 select a,
count(b),
sum(case b when 0 then 1 else 0 end),
sum(case b when 1 then 1 else 0 end),
sum(case b when 1 then 1 else 0 end) / count(b) *100.000 || '%'
from aaa
group by a
order by a;
或者
select a,
count(b),
sum(case b when 0 then 1 else 0 end),
sum(b),
sum(b) / count(b) *100.000 || '%'
from aaa
group by a
order by a;
或者
select a,
count(b),
count(b) - sum(b),
sum(b),
sum(b) / count(b) *100.000 || '%'
from aaa
group by a
order by a
[ 本帖最后由 xxyyy 于 2008-5-23 07:44 编辑 ]
回复 #3 xxyyy 的帖子
很好很强大.......:em17: :em17:回复 #3 xxyyy 的帖子
佩服!
页:
[1]