查询每个学生的平均点数

时间:2017-06-02 08:06:28

标签: sql json postgresql

我有两张桌子:

学生:

   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;

但仍然不知道如何继续。这可能是糟糕的解决方案。

3 个答案:

答案 0 :(得分:2)

试试这个:

select student_id
, avg((marks->>m)::float) average
, count(distinct sem) number_of_sems
from semestr s
join student t on s.student_id = t.id
left outer join json_object_keys(marks) m on true
group by student_id;
 student_id |     average      | number_of_sems
------------+------------------+----------------
          2 | 3.83333333333333 |              3
         13 |                4 |              2
(3 rows)

<强>更新

正如波兹所说的那样 - 我们可能应该计算没有考试的学期作为semestres ...

答案 1 :(得分:2)

这包括没有分数的学生:

SELECT st.id, avg(m.value)
FROM student st
   LEFT JOIN semester se
      ON st.id = se.student_id
   LEFT JOIN LATERAL (SELECT value::numeric
                      FROM jsonb_each_text(se.marks)
                     ) m
      ON TRUE
GROUP BY st.id;

┌────┬────────────────────┐
│ id │        avg         │
├────┼────────────────────┤
│  2 │ 3.8333333333333333 │
│ 13 │ 4.0000000000000000 │
│ 22 │                    │
└────┴────────────────────┘
(3 rows)

答案 2 :(得分:0)

感谢大家使用json_each_text()和avg()函数的想法。 因此,如果目的是在1名学生的每个科目中获得平均分数:

SELECT 
     st.id,
     json_data.key AS subject, 
     SUM(json_data.value::DOUBLE PRECISION) AS sum_value,
     avg(json_data.value::DOUBLE PRECISION) AS avg_value
FROM student  AS st,
json_each_text(st.marks::JSON) AS json_data
GROUP BY si.id, subject;