我对SQL非常陌生
例如,一个具有2 As和3 C的学生将产生两条记录:sid,A,2; sid,C,3。如果学生没有任何成绩,我不想列出他们
select sid, count(grade) from enrollments
group by sid
我想要的例子:
sid grade
123 A,2; C,3
456 A,3; B,1
答案 0 :(得分:1)
我认为这应该对您有用。
select sid,
listagg(gradecount, '; ') within group (order by gradecount) as grade
from (
select sid, grade || ',' || count(1) as gradecount
from enrollments
where grade is not null
group by sid, grade
) studentgradecount
group by sid;
如果看起来令人困惑,请尝试单独运行内部查询以查看其作用:
select sid, grade || ',' || count(1) as gradecount
from enrollments
where grade is not null
group by sid, grade