我有2个表:查询平均点每个学生
学生:
id name
2 ABC
13 DEF
22 GHI
学期:
id student_id sem marks
1 2 1 {"math":3, "physic":4, "chemis":5}
2 2 2 {"math":2.5, "physic":4.5, "chemis":5}
3 2 3 {"math":3, "physic":3.5, "chemis":4}
5 13 1 {"math":3, "physic":4, "chemis":5}
6 13 2 {"math":3, "physic":4, "chemis":5}
因此,例如用student_id数据= 2:
平均分数=( (3 + 4 + 5)/ 3)+(2.5 + 4.5 + 5)/ 3 +(3 + 3.5 + 4)/ 3)/3=3.83
有没有办法按学生ID查询平均分数组?
student_id average number_of_sems
2 3.83 3
13 xxx 2
我试图通过主题来算:
SELECT
t1.student_id,
t1.count,
(SELECT sum(xx.count)
FROM
(SELECT (marks:: JSON ->> 'math') :: DOUBLE PRECISION AS count
FROM "Semester"
WHERE student_id= t1.student_id) AS xx)
FROM
(
SELECT
student_id,
count(1)
FROM "Semester"
GROUP BY student_id
) AS t1;
但仍然不知道如何继续。这可能是不好的解决方案。
谢谢你提醒平均 - 我分数和:) –