基础SQL
ID名称数量
--------------------
1110
2123
3215
4316
5418
6220
7145
8320
9421
---------------------
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%