再问统计多列百分比
查询得到如下结果:
f g r 数量
-1 -1 -1 25
-1 -1 0 15
-1 -1 1 10
-1 0 -1 18
-1 0 0 3
-1 0 1 13
-1 1 -1 24
-1 1 0 6
-1 1 1 7
0 -1 -1 34
0 -1 0 17
0 -1 1 18
0 0 -1 23
0 0 0 10
0 0 1 13
0 1 -1 18
0 1 0 13
0 1 1 13
1 -1 -1 18
1 -1 0 21
1 -1 1 9
1 0 -1 16
1 0 0 9
1 0 1 11
1 1 -1 15
1 1 0 4
1 1 1 18
F/G/R分别有三种组合-1,0,1 数量就是普通的整数> 0
希望得到下面的结果:
-1 -1 -1 50%
-1 -1 0 30%
-1 -1 1 20%
-1 0 -1 53%
-1 0 0 9%
-1 0 1 38%
-1 1 -1 65%
-1 1 0 16%
-1 1 1 19%
... ... ... ...
把27种F/G/R排列分别算出来.
F/G不变,R变化,F/G/R三种排列所占的百分比
------解决方案--------------------create table tab(f varchar(2),g varchar(2),r varchar(2),数量 int)
insert tab
select '-1 ', '-1 ', '-1 ',25
union all
select '-1 ', '-1 ', '0 ',15
union all
select '-1 ', '-1 ', '1 ',10
union all
select '-1 ', '0 ', '-1 ',18
union all
select '-1 ', '0 ', '0 ',3
union all
select '-1 ', '0 ', '1 ',13
union all
select '-1 ', '1 ', '-1 ',24
union all
select '-1 ', '1 ', '0 ',6
union all
select '-1 ', '1 ', '1 ',7
union all
select '0 ', '-1 ', '-1 ',34
union all
select '0 ', '-1 ', '0 ',17
union all
select '0 ', '-1 ', '1 ',18
union all
select '0 ', '0 ', '-1 ',23
union all
select '0 ', '0 ', '0 ',10
union all
select '0 ', '0 ', '1 ',13
union all
select '0 ', '1 ', '-1 ',18
union all
select '0 ', '1 ', '0 ',13
union all
select '0 ', '1 ', '1 ',13
union all
select '1 ', '-1 ', '-1 ',18
union all
select '1 ', '-1 ', '0 ',21
union all
select '1 ', '-1 ', '1 ',9
union all
select '1 ', '0 ', '-1 ',16
union all
select '1 ', '0 ', '0 ',9
union all
select '1 ', '0 ', '1 ',11
union all
select '1 ', '1 ', '-1 ',15
union all
select '1 ', '1 ', '0 ',4
union all
select '1 ', '1 ', '1 ',18
select f,g,r,cast(sum(数量)*100/(select sum(数量) from tab t1 where t1.f=t2.f and t1.g=t2.g) AS varchar(10))+ '% ' from tab t2
group by f,g,r
order by f desc,g desc,r desc
drop table tab