query with LISTAGG() string function does not return the desired output
-
05-02-2021 - |
题
I'm new to ORACLE and I'm trying to use a string aggregation function in my query to concatenate the value of rows with a comma ,
this is the structure of my tables:
- Student(STUDENT_ID,student_name,age)
- Course(course_no,description)
- Student_Course(student_id,course_id,nomreh)
description column indicates the name of the course for example Algebra or Math. Nomreh column shows the Mark each student achieved in each course (it's a Persian word).
This is my query
Select s.student_name ,
LISTAGG(c.description, ',') WITHIN GROUP (ORDER BY c.description) AS
Courses,
LISTAGG(sc.nomreh, ',') WITHIN GROUP (ORDER BY sc.nomreh) AS Ranks
from student s inner join
student_course sc on s.student_id = sc.student_id
inner join
course c on sc.course_id = c.course_no
group by s.student_name
I want the output to show:
[Student-name] [Course-Description] [Nomre]
Artin Algebra,Math,Sport 10,11,12
each value in [Nomreh]
column should be exactly for that [Course]
column and for that specific student . Unfortunately my query does not give the desired output and the values in Nomreh column are not in correct order with [Course-Description] column.
解决方案
Change this part of your query to the below:
LISTAGG(c.description, ',') WITHIN GROUP (ORDER BY
s.student_name,c.description,sc.nomreh) AS Courses,
LISTAGG(sc.nomreh, ',') WITHIN GROUP (ORDER BY
s.student_name,c.description,sc.nomreh) AS Ranks
不隶属于 dba.stackexchange