我有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;
但仍然不知道如何繼續。這可能是不好的解決方案。
謝謝你提醒平均 - 我分數和:) –