SQL ROWS CONCAT + JOIN

时间:2018-02-20 17:14:17

标签: sql oracle

您好我使用的是Oracle SQL,我希望消除重复的行(使用相同的教师)。我知道这是一项简单的任务,但我有点卡住......请帮助我。

SELECT a.name||' '|| a.first_name AS teacher_name,
       c.course_title AS courses 
FROM teacher a JOIN
     didactic b
     ON a.id_prof = b.id_prof JOIN
     courses c 
     ON b.id_course = c.id_cours 
ORDER BY teacher_name;

这是输出:

teacher_name    courses
A                sport
A                math
B                one
B                two

1 个答案:

答案 0 :(得分:1)

您正在寻找listagg()

SELECT a.name || ' ' || a.first_name AS teacher_name,
       LISTAGG(c.course_title, ',') WITHIN GROUP (ORDER BY c.course_title) AS courses 
FROM teacher a JOIN
     didactic b
     ON a.id_prof = b.id_prof JOIN
     courses c 
     ON b.id_course = c.id_cours
GROUP BY a.name || ' ' || a.first_name
ORDER BY teacher_name;