日期:2014-05-17  浏览次数:20753 次

基础SQL
ID 名称 数量
--------------------
1 1 10
2 1 23
3 2 15
4 3 16
5 4 18
6 2 20
7 1 45
8 3 20
9 4 21
---------------------
1.查询名称为1的总数量与所有总数量的百分比(一条Sql语句)
2.查询各个名称的总数量与所有总数量的百分比(一条Sql语句)

------解决方案--------------------
你的数据我看得不是很明白,给你举个例子~~

表tt:

ID NAME QUANTITY
---------- ---- ----------
1 aa 20
2 aa 30
3 aa 20
4 bb 50
5 bb 80
6 aa 10
7 cc 20
8 aa 60


1.查询name为aa的总数量与所有总数量的百分比(一条Sql语句)

=======================sql=================================

select *
from (
select tt.name,
round(RATIO_TO_REPORT(sum(tt.quantity)) over() * 100,2) || '% ' as percent
from tt
group by tt.name
)zz
where zz.name = 'aa ';


======================result===============================

NAME PERCENT
---- -----------------------------------------
aa 48.28%


2.查询各个name的总数量与所有总数量的百分比(一条Sql语句)

=======================sql=================================

select tt.name,
round(RATIO_TO_REPORT(sum(tt.quantity)) over() * 100,2) || '% ' as percent
from tt
group by tt.name;


======================result===============================

NAME PERCENT
---- -----------------------------------------
aa 48.28%
bb 44.83%
cc 6.9%