ListAgg()中的CASE表达式

时间:2019-07-29 19:19:04

标签: sql oracle case listagg

我使用案例表达式在查询中包含ListAgg。

但是,我一直收到错误“缺少关键字”。我使用了在其他答案中找到的格式,但是仍然无法使用。

    Select
    SC.NAME,
    listagg(case sc.COURSE_REFERENCE_NUMBER
    when  sc.department = 'AVSC' and sc.course_number in ('100', '200', 
    '300', '400') end, ', ')
    within group (order by sc.COURSE_REFERENCE_NUMBER) as avsc_crn

    from odsmgr.student_course SC
    where SC.ACADEMIC_PERIOD = '201990'

确切的消息是:

  

服务器错误号:003000
  OraOLEDB:ORA-00905:缺少关键字

3 个答案:

答案 0 :(得分:1)

您的case语法已关闭。您需要一个then子句:

listagg(case when  sc.department = 'AVSC' and sc.course_number in ('100', '200', '300', '400')
             then sc.COURSE_REFERENCE_NUMBER
         end, ', ') within group (order by sc.COURSE_REFERENCE_NUMBER) as avsc_crn

答案 1 :(得分:0)

CASE语句在语法上是错误的。
我认为您想要的是这个

case 
    when sc.department = 'AVSC' and sc.course_number in ('100', '200', '300', '400') 
      then sc.COURSE_REFERENCE_NUMBER
end

也许您需要添加一个ELSE零件:

case 
    when sc.department = 'AVSC' and sc.course_number in ('100', '200', '300', '400') 
      then sc.COURSE_REFERENCE_NUMBER
    else ....
end

答案 2 :(得分:0)

似乎您没有SC.COURSE_REFERENCE_NUMBER的THEN条件

Select
  SC.NAME,
  listagg(
    case   when  sc.department = 'AVSC' and sc.course_number in ('100', '200', 
  '300', '400')  THEN sc.COURSE_REFERENCE_NUMBER end, ', ')
  within group (order by sc.COURSE_REFERENCE_NUMBER) as avsc_crn
  from odsmgr.student_course SC
  where SC.ACADEMIC_PERIOD = '201990'