列 SQL 百分比的总和
Posted
技术标签:
【中文标题】列 SQL 百分比的总和【英文标题】:Sum total of a column SQL Percentage 【发布时间】:2020-06-11 13:44:15 【问题描述】:我正在尝试将最终记录作为计数和百分比的总和:
SELECT region, count(*) as total, concat(to_char(round(count(*) * 100/ sum(count(*)) over() ,2),'990.99'),
'%') perc
FROM (SELECT
CASE
WHEN Substr(veh_vin, 1, 1) >= 'A'
AND Substr(veh_vin, 1, 1) <= 'C' THEN 'America'
WHEN Substr(veh_vin, 1, 1) >= 'J'
AND Substr(veh_vin, 1, 1) <= 'R' THEN 'Poland'
WHEN Substr(veh_vin, 1, 1) >= 'S'
AND Substr(veh_vin, 1, 1) <= 'Z' THEN 'Euro'
WHEN Substr(veh_vin, 1, 1) BETWEEN 1 AND 5 THEN 'North Africa'
WHEN Substr(veh_vin, 1, 1) BETWEEN 6 AND 7 THEN 'Australia'
WHEN Substr(veh_vin, 1, 1) BETWEEN 8 AND 9 THEN 'China'
ELSE 'UNKNOWN'
END AS region
FROM vehicle) s
GROUP BY region
UNION
SELECT 'Total' Region,
Count(*) TOTAL,
concat(to_char(count(*) * 100/ sum(count(*)) over(),'990.99'),
'%') perc
FROM vehicle ;
我明白了:
Region TOTAL PERC
America 66 31.28%
Poland 89 42.18%
Europe 1 .47%
Australia 1 .47%
China 54 25.59%
Total 211 100.00%
预计是:
Region TOTAL PERC
America 66 31.28%
Poland 89 42.18%
Europe 1 0.47%
Australia 1 0.47%
China 54 25.59%
Total 211 99.99%
我正在尝试将 99.99 的值作为 perc 列的总和,我该怎么做?
【问题讨论】:
为什么要 99.99 而不是 100.00%? 尝试实际计算总百分比是没有意义的。你知道它已经是 100% 了,那为什么不直接使用文字值呢?count(*) * 100/ sum(count(*)) over()
返回一个int
,所以你做的回合没有效果,试试round(count(*) * 100.0/ sum(count(*)) over() ,2)
(通知100.0),是你的问题吗?
这就是我要找的要求! @戈登林诺夫
@nfgl 不,我编辑了我的问题,我得到 100.00,我需要 99.99,这是 Perc 列的总和。
【参考方案1】:
这符合要求
with regions as (
SELECT region, count(*) as total, round(count(*) * 100/ sum(count(*)) over(),2) perc
FROM (SELECT
CASE
WHEN Substr(veh_vin, 1, 1) >= 'A'
AND Substr(veh_vin, 1, 1) <= 'C' THEN 'America'
WHEN Substr(veh_vin, 1, 1) >= 'J'
AND Substr(veh_vin, 1, 1) <= 'R' THEN 'Poland'
WHEN Substr(veh_vin, 1, 1) >= 'S'
AND Substr(veh_vin, 1, 1) <= 'Z' THEN 'Euro'
WHEN Substr(veh_vin, 1, 1) BETWEEN 1 AND 5 THEN 'North Africa'
WHEN Substr(veh_vin, 1, 1) BETWEEN 6 AND 7 THEN 'Australia'
WHEN Substr(veh_vin, 1, 1) BETWEEN 8 AND 9 THEN 'China'
ELSE 'UNKNOWN'
END AS region
FROM vehicle) s
GROUP BY region )
select region, total, concat(to_char(perc,'990.99'), '%') perc from regions
union all
select 'Total' region, sum(Total) total, concat(to_char(sum(Perc),'990.99'), '%') perc
from regions
【讨论】:
以上是关于列 SQL 百分比的总和的主要内容,如果未能解决你的问题,请参考以下文章