简体   繁体   English

Oracle Listagg子查询

[英]Oracle Listagg Sub query

SELECT e.pem_id          AS id,
     e.pem_subject     AS subject,
     e.pem_content     AS content,
     e.pem_sent_date   AS sentdate,
     e.pem_ref_id      AS referenceid,
     e.pem_from_usr_id AS userid,
     NULL              AS misc,
     (listagg(str.str_us_id, ', ') within GROUP(ORDER BY '') AS attachedusers FROM
      proj_email_usrs eu LEFT OUTER JOIN st_register str ON
      eu.pmu_str_id = str.str_id WHERE eu.pmu_pem_id = '26' GROUP BY '')
FROM   proj_email e
WHERE  e.pem_prj_id = '33'
AND    e.pmu_pem_id = '26'
AND    e.pem_status = 'S';

It is throwing error as 它正在抛出错误

ORA-01722: invalid number ORA-01722:无效的号码

01722. 00000 - "invalid number" 01722. 00000 - “无效号码”

*Cause: The specified number was invalid. *原因:指定的号码无效。

*Action: Specify a valid number. *操作:指定有效数字。

I assume you want a query like this: 我假设您想要这样的查询:

SELECT E.PEM_ID as Id, E.PEM_SUBJECT as Subject, E.PEM_CONTENT as Content,
       E.PEM_SENT_DATE as SentDate, E.PEM_REF_ID as ReferenceId,   
       E.PEM_FROM_USR_ID as UserId, NULL as Misc,
       (SELECT LISTAGG(STR.STR_US_ID, ', ') WITHIN GROUP (ORDER BY STR.STR_US_ID)
        FROM PROJ_EMAIL_USRS EU LEFT OUTER JOIN
             ST_REGISTER STR
             ON EU.PMU_STR_ID = STR.STR_ID
        WHERE EU.PMU_PEM_ID = E.PMU_PEM_ID  -- Correlation clause
       ) as AttachedUsers
FROM PROJ_EMAIL E 
WHERE E.PEM_PRJ_ID = 33 AND E.PMU_PEM_ID = 26 AND E.PEM_STATUS = 'S' ;

Comments: 评论:

  • Added the SELECT before LISTAGG() . LISTAGG()之前添加了SELECT Needed for a subquery. 子查询需要。
  • Remove GROUP BY . 删除GROUP BY Not needed, because you want the subquery to always return one row. 不需要,因为您希望子查询始终返回一行。
  • Removed single quotes from constants that are probably numbers. 从可能是数字的常量中删除了单引号。 Only use single quotes for string and date constants. 仅对字符串和日期常量使用单引号。
  • Added a correlation clause to the subquery. 在子查询中添加了关联子句。
  • Moved the name of the column outside the subquery, so it is named in the result. 在子查询外部移动了列的名称,因此在结果中命名。
  • Added an ORDER BY column for WITHIN GROUP . WITHIN GROUP添加了ORDER BY列。 Not necessary, but reasonable to keep the ids in order. 没有必要,但保持ids井井有条。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM