如何计算最终总和并计算百分比
How to calculate the Final Total sum and calculate percentages
下面我有一个查询,其中我有每种语言的学生使用的第一种语言总数
CTE
WITH Lang AS
(
SELECT
language,
studentcount,
SUM(Studentcount) AS total
FROM
(SELECT
l.longtext AS language,
COUNT(distinct s.studentnr) AS Studentcount
FROM
student s
JOIN
pupil p on p.id = s.pupilid
JOIN
pupillanguage pl on pl.personid = p.id
JOIN
language l on l.id = pl.languageid
GROUP BY
l.longtext
ORDER BY
Studentcount DESC
) t
GROUP BY
language, Studentcount
)
查询
SELECT initcap(language), Studentcount, total
FROM Lang
UNION ALL
SELECT cast(count(language) as varchar(6)) ||' Languages', null, null
FROM Lang
现在我有 1 个主要问题是分配学生的 TOTAL SUM 值。我需要这个,所以我可以计算列中 学生人数/总学生人数 的 百分比 。然而显然我的总价值并没有给我我需要的东西。
输出
languages students total
------------------------------
French 734 734
Afrikaans 93 93
Greek 117 117
German 55 55
Armenian 160 160
Malaysian 5 5
Danish 15 15
American 5 5
Swedish 24 24
Bulgarian 1043 1043
预期输出:
languages students Percentage
-----------------------------------
French 734 24,46
Afrikaans 93 3,12
Greek 117 3,9
German 55 1,83
Armenian 160 5,33
Malaysian 5 0,16
Danish 15 0,5
American 5 0,16
Swedish 24 0,8
Bulgarian 1043 34,76
如何计算最终总和作为计算百分比的值
您可以使用 window 函数 (https://www.postgresql.org/docs/current/static/tutorial-window.html):
SELECT
language,
students,
SUM(students) OVER () as total,
students::decimal / SUM(students) OVER () * 100 as percentage
FROM
--- <YOUR QUERY>
window 函数 SUM(students) OVER ()
对学生列的所有值求和,因为 OVER
子句中没有给出限制框
::decimal
转换是获得浮点除法 (100 / 200 = 0.5) 而不是整数除法 (100 / 200 = 0) 所必需的。
你可以试试下面的方法
with t1 as (
SELECT l.longtext as language,
count(distinct s.studentnr) as Studentcount
FROM student s
join pupil p on p.id = s.pupilid
join pupillanguage pl on pl.personid = p.id
join language l on l.id = pl.languageid
group by l.longtext
order by Studentcount desc
) select language,Studentcount, sum(Studentcount) over() as total,
(Studentcount::float/sum(Studentcount) over())*100 as percentage
from t1
下面我有一个查询,其中我有每种语言的学生使用的第一种语言总数
CTE
WITH Lang AS
(
SELECT
language,
studentcount,
SUM(Studentcount) AS total
FROM
(SELECT
l.longtext AS language,
COUNT(distinct s.studentnr) AS Studentcount
FROM
student s
JOIN
pupil p on p.id = s.pupilid
JOIN
pupillanguage pl on pl.personid = p.id
JOIN
language l on l.id = pl.languageid
GROUP BY
l.longtext
ORDER BY
Studentcount DESC
) t
GROUP BY
language, Studentcount
)
查询
SELECT initcap(language), Studentcount, total
FROM Lang
UNION ALL
SELECT cast(count(language) as varchar(6)) ||' Languages', null, null
FROM Lang
现在我有 1 个主要问题是分配学生的 TOTAL SUM 值。我需要这个,所以我可以计算列中 学生人数/总学生人数 的 百分比 。然而显然我的总价值并没有给我我需要的东西。
输出
languages students total
------------------------------
French 734 734
Afrikaans 93 93
Greek 117 117
German 55 55
Armenian 160 160
Malaysian 5 5
Danish 15 15
American 5 5
Swedish 24 24
Bulgarian 1043 1043
预期输出:
languages students Percentage
-----------------------------------
French 734 24,46
Afrikaans 93 3,12
Greek 117 3,9
German 55 1,83
Armenian 160 5,33
Malaysian 5 0,16
Danish 15 0,5
American 5 0,16
Swedish 24 0,8
Bulgarian 1043 34,76
如何计算最终总和作为计算百分比的值
您可以使用 window 函数 (https://www.postgresql.org/docs/current/static/tutorial-window.html):
SELECT
language,
students,
SUM(students) OVER () as total,
students::decimal / SUM(students) OVER () * 100 as percentage
FROM
--- <YOUR QUERY>
window 函数 SUM(students) OVER ()
对学生列的所有值求和,因为 OVER
子句中没有给出限制框
::decimal
转换是获得浮点除法 (100 / 200 = 0.5) 而不是整数除法 (100 / 200 = 0) 所必需的。
你可以试试下面的方法
with t1 as (
SELECT l.longtext as language,
count(distinct s.studentnr) as Studentcount
FROM student s
join pupil p on p.id = s.pupilid
join pupillanguage pl on pl.personid = p.id
join language l on l.id = pl.languageid
group by l.longtext
order by Studentcount desc
) select language,Studentcount, sum(Studentcount) over() as total,
(Studentcount::float/sum(Studentcount) over())*100 as percentage
from t1