SQL根据表中的现有数据计算百分比并进行更新

时间:2021-01-02 08:04:35

I have table as below with all the fields except percentage. Based on the total Number of cases I need to calculate percentage for each case id. For easy interpretation I choose numbers as below. So the total cases for all the caseids is 100, out of which case id as 35 cases so its percentage is 35. What script can I use to get those total percentage values and update in table accordingly.

我有以下表格,除了百分比以外的所有字段。基于案例总数我需要计算每个案例id的百分比。为了便于解释,我选择数字如下。因此,所有caseid的总案例为100,其中id为35个案例,因此其百分比为35.我可以使用哪个脚本来获取这些总百分比值并相应地更新表格。

CaseID NumHighCases NumMedCases NumLowCases TotalCases TotalPerc
1         10            20         5           35          35%
2         5              5          5          15          15%
3         8              12         20         40          40%
4         3               4          3         10          10%

1 个解决方案

#1


4  

TotalPerc = (TotalCases*100.0)/sum(TotalCases) over (partition by null)

Note the *100 is to add precision, otherwise an int/int will return an int.

注意* 100是为了增加精度,否则int / int将返回一个int。

#1


4  

TotalPerc = (TotalCases*100.0)/sum(TotalCases) over (partition by null)

Note the *100 is to add precision, otherwise an int/int will return an int.

注意* 100是为了增加精度,否则int / int将返回一个int。