将 SELECT 结果与另一个 table 的值相乘
multiply SELECT results with values from another table
我有 3 个 table 来自不同学校的学生数据。我应该在 table(done) 中计算每所学校的学生人数,然后使用给定的概率计算出每组概率有多少学生。我从另一个查询中获得了每所学校学生的结果。
示例:我使用查询来查找学校的学生总数 "CAA"。
如果是198,那么概率分布就是
(0.05*198、0.08*198、0.18*198、0.3*198、0.11*198、0.28*198)。
我如何从 table 中获取这些值并与我的结果相乘?
我希望这是有道理的,请原谅我是 Postgres 的新手。我已附上我的查询以获取两个相关 table 的计数和照片。
simulated_records:
record_id(PK bigint) Status(text) grade(text
1 CL -
2 CEC -
3 CEC -
4 CEC -
5 CAA -
6 CAS -
7 CAA -
8 CAA -
9 CAA -
10 CL -
school_probs:
school_code(PK bigint) school(text) probs(numeric)
1 CAA {0.05,0.08,0.18,0.3,0.11,0.28}
2 CAS {0.06,0.1,0.295,0.36,0.12,0.065}
3 CBA {0.05,0.11,0.35,0.32,0.12,0.05}
4 CL {0.07,0.09,0.24,0.4,0.06,0.09}
grade_values:
id(PK integer) score(text) grade(text
1 95-100 A
2 90-94 A-
3 80-89 B+
4 70-79 B
5 60-69 C
6 0-59 D
我的查询:
SELECT simulated_records.school, COUNT(simulated_records.school) as CountSchool
FROM simulated_records, school_probs
WHERE simulated_records.school = school_probs.school
GROUP BY simulated_records.school;
我应该能够做的是根据每所学校的学生数量并使用概率(使用函数分解多少孩子应该有什么成绩)在模拟记录中填充成绩table .
另外实际模拟记录table有2000多条记录
预期结果:simulated_records 中的空成绩列将根据学生人数和 school_probs 的概率填充成绩。
record_id(PK bigint) Status(text) grade(text
1 CL A
2 CEC B
3 CEC B
4 CEC A
5 CAA C
6 CAS B
7 CAA D
8 CAA A
9 CAA A
10 CL C
不确定您期望的结果是什么,但这里是:
select school, factor, CountSchool
from (
SELECT simulated_records.school, COUNT(simulated_records.school) as CountSchool
FROM simulated_records, school_probs
WHERE simulated_records.school = school_probs.school
GROUP BY simulated_records.school
) eq
cross join (
values
(0.05), (0.08), (0.18), (0.3), (0.11), (0.28)
) t (factor)
这会产生这样的结果:
school factor count_of
1 CAA 0.0500 198
2 CAA 0.0800 198
3 CAA 0.1800 198
4 CAA 0.3000 198
5 CAA 0.1100 198
6 CAA 0.2800 198
nb:您尚未说明将如何存储 "factor" 信息。
SELECT
school,
json_object_agg(gv.grade, s.students) -- 4
FROM (
SELECT
s.school,
ceil(unnest(sp.probs) * s.students_per_school) students, -- 2
generate_series(1,6) gs -- 3
FROM (
SELECT 'CAA'::text as school, 198 as students_per_school -- 1
) s
JOIN school_probs sp ON s.school = sp.school
) s
JOIN grade_values gv ON gv.id = s.gs
GROUP BY school
结果:
school json_object_agg
CAA { "A" : 10, "A-" : 16, "B+" : 36, "B" : 60, "C" : 22, "D" : 56 }
希望这是您期望的结果:
- 从某处获取数据(模拟您的 table 或子查询或其他)
unnest
将您的 numeric
probs
数组扩展为每个元素一行。这是主要技巧
- 与您的学生相乘
ceil
向上舍入(否则你可以做 floor
向下舍入或 round
进行正常舍入)
generate_series
添加一个 integer
列以便能够加入 grade_values
table
- 现在每个学校的每个年级都有一行(请参阅下面的编辑)。如果这是您的预期结果,就是这样。如果您想将每个学校的成绩值汇总到一行中,您可以这样做
json_object_agg
编辑:没有聚合的版本:
SELECT
school,
gv.grade,
s.students
FROM (
SELECT
s.school,
ceil(unnest(sp.probs) * s.students_per_school) students,
generate_series(1,6) gs
FROM (
SELECT 'CAA'::text as school, 198 as students_per_school
) s
JOIN school_probs sp ON s.school = sp.school
) s
JOIN grade_values gv ON gv.id = s.gs
结果:
school grade students
CAA A 10
CAA A- 16
CAA B+ 36
CAA B 60
CAA C 22
CAA D 56
我有 3 个 table 来自不同学校的学生数据。我应该在 table(done) 中计算每所学校的学生人数,然后使用给定的概率计算出每组概率有多少学生。我从另一个查询中获得了每所学校学生的结果。
示例:我使用查询来查找学校的学生总数 "CAA"。 如果是198,那么概率分布就是 (0.05*198、0.08*198、0.18*198、0.3*198、0.11*198、0.28*198)。 我如何从 table 中获取这些值并与我的结果相乘?
我希望这是有道理的,请原谅我是 Postgres 的新手。我已附上我的查询以获取两个相关 table 的计数和照片。
simulated_records:
record_id(PK bigint) Status(text) grade(text
1 CL -
2 CEC -
3 CEC -
4 CEC -
5 CAA -
6 CAS -
7 CAA -
8 CAA -
9 CAA -
10 CL -
school_probs:
school_code(PK bigint) school(text) probs(numeric)
1 CAA {0.05,0.08,0.18,0.3,0.11,0.28}
2 CAS {0.06,0.1,0.295,0.36,0.12,0.065}
3 CBA {0.05,0.11,0.35,0.32,0.12,0.05}
4 CL {0.07,0.09,0.24,0.4,0.06,0.09}
grade_values:
id(PK integer) score(text) grade(text
1 95-100 A
2 90-94 A-
3 80-89 B+
4 70-79 B
5 60-69 C
6 0-59 D
我的查询:
SELECT simulated_records.school, COUNT(simulated_records.school) as CountSchool
FROM simulated_records, school_probs
WHERE simulated_records.school = school_probs.school
GROUP BY simulated_records.school;
我应该能够做的是根据每所学校的学生数量并使用概率(使用函数分解多少孩子应该有什么成绩)在模拟记录中填充成绩table .
另外实际模拟记录table有2000多条记录
预期结果:simulated_records 中的空成绩列将根据学生人数和 school_probs 的概率填充成绩。
record_id(PK bigint) Status(text) grade(text
1 CL A
2 CEC B
3 CEC B
4 CEC A
5 CAA C
6 CAS B
7 CAA D
8 CAA A
9 CAA A
10 CL C
不确定您期望的结果是什么,但这里是:
select school, factor, CountSchool
from (
SELECT simulated_records.school, COUNT(simulated_records.school) as CountSchool
FROM simulated_records, school_probs
WHERE simulated_records.school = school_probs.school
GROUP BY simulated_records.school
) eq
cross join (
values
(0.05), (0.08), (0.18), (0.3), (0.11), (0.28)
) t (factor)
这会产生这样的结果:
school factor count_of
1 CAA 0.0500 198
2 CAA 0.0800 198
3 CAA 0.1800 198
4 CAA 0.3000 198
5 CAA 0.1100 198
6 CAA 0.2800 198
nb:您尚未说明将如何存储 "factor" 信息。
SELECT
school,
json_object_agg(gv.grade, s.students) -- 4
FROM (
SELECT
s.school,
ceil(unnest(sp.probs) * s.students_per_school) students, -- 2
generate_series(1,6) gs -- 3
FROM (
SELECT 'CAA'::text as school, 198 as students_per_school -- 1
) s
JOIN school_probs sp ON s.school = sp.school
) s
JOIN grade_values gv ON gv.id = s.gs
GROUP BY school
结果:
school json_object_agg
CAA { "A" : 10, "A-" : 16, "B+" : 36, "B" : 60, "C" : 22, "D" : 56 }
希望这是您期望的结果:
- 从某处获取数据(模拟您的 table 或子查询或其他)
unnest
将您的numeric
probs
数组扩展为每个元素一行。这是主要技巧- 与您的学生相乘
ceil
向上舍入(否则你可以做floor
向下舍入或round
进行正常舍入)
generate_series
添加一个integer
列以便能够加入grade_values
table- 现在每个学校的每个年级都有一行(请参阅下面的编辑)。如果这是您的预期结果,就是这样。如果您想将每个学校的成绩值汇总到一行中,您可以这样做
json_object_agg
编辑:没有聚合的版本:
SELECT
school,
gv.grade,
s.students
FROM (
SELECT
s.school,
ceil(unnest(sp.probs) * s.students_per_school) students,
generate_series(1,6) gs
FROM (
SELECT 'CAA'::text as school, 198 as students_per_school
) s
JOIN school_probs sp ON s.school = sp.school
) s
JOIN grade_values gv ON gv.id = s.gs
结果:
school grade students
CAA A 10
CAA A- 16
CAA B+ 36
CAA B 60
CAA C 22
CAA D 56