我有3张桌子,上面有不同学校的学生数据。我应该在一张表(完成)中获得每所学校的学生人数,然后使用给定的概率计算出每套概率有多少学生。我从另一个查询中得到了每所学校的学生结果。
例如:我使用查询来查找“ CAA”学校的学生总数。 如果为198,则概率分布为 (0.05 * 198、0.08 * 198、0.18 * 198、0.3 * 198、0.11 * 198、0.28 * 198)。 如何从上述表格中获取这些值并与结果相乘?
我希望这是有道理的,请原谅我是Postgres的新手。我已附加查询以获取两个相关表的计数和照片。
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;
我应该能够做的是根据每所学校的学生人数和概率来填充模拟记录表中的成绩(使用函数可以分解多少个孩子应达到的成绩。
实际的模拟记录表也有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
答案 0 :(得分:1)
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 }
希望这是您期望的结果:
unnest
将numeric
probs
数组扩展为每个元素一行。这是主要技巧ceil
进行四舍五入(否则,您可以进行floor
进行四舍五入,或者执行round
进行常规四舍五入)generate_series
添加了一个integer
列,以便能够连接grade_values
表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
答案 1 :(得分:0)
不确定结果如何,但是请按以下步骤操作:
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:您尚未说明如何存储“因子”信息。