Oracle-11g

使用 LISTAGG() 字元串函式查詢不返回所需的輸出

  • August 28, 2020

我是 ORACLE 的新手,我正在嘗試在查詢中使用字元串聚合函式來用逗號連接行的值,

這是我的表的結構:

  1. 學生(STUDENT_ID,學生姓名,年齡)
  2. 課程(course_no,描述)
  3. Student_Course(student_id,course_id,nomreh)

描述列指示課程名稱,例如代數或數學。Nomreh 列顯示每個學生在每門課程中取得的分數(這是一個波斯語單詞)。

這是我的查詢

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 

我希望輸出顯示:

[Student-name]   [Course-Description]    [Nomre]
 Artin           Algebra,Math,Sport    10,11,12

列中的每個值[Nomreh]都應完全針對該[Course]列和該特定學生。不幸的是,我的查詢沒有給出所需的輸出,並且 Nomreh 列中的值的順序不正確

$$ Course-Description $$柱子。

將查詢的這一部分更改為以下內容:

      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

引用自:https://dba.stackexchange.com/questions/240622