是否可以使用 count 和 group by 获取查询的百分比值?
Is possible to get the percentage value on query using count and group by?
我有一个问题:
select no_tipo_origem,
count(*) total
from table
group by no_tipo_origem
结果是:
tipo 1 | 25
tipo 2 | 133
tipo 3 | 48
我需要这样计算百分比:
tipo 1 | 25 | 12.1
tipo 2 | 133 | 64.5
tipo 3 | 48 | 23.3
用 SUM()
window 函数可以得到的所有总和除以每个总和:
select no_tipo_origem,
count(*) total,
round(100.0 * count(*) / sum(count(*)) over (), 1) percentage
from tablename
group by no_tipo_origem
order by no_tipo_origem
查看简化版 demo。
我有一个问题:
select no_tipo_origem,
count(*) total
from table
group by no_tipo_origem
结果是:
tipo 1 | 25
tipo 2 | 133
tipo 3 | 48
我需要这样计算百分比:
tipo 1 | 25 | 12.1
tipo 2 | 133 | 64.5
tipo 3 | 48 | 23.3
用 SUM()
window 函数可以得到的所有总和除以每个总和:
select no_tipo_origem,
count(*) total,
round(100.0 * count(*) / sum(count(*)) over (), 1) percentage
from tablename
group by no_tipo_origem
order by no_tipo_origem
查看简化版 demo。