暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle listagg给出ORA-01427: 单行子查询返回多行

askTom 2017-08-03
982

问题描述

我需要将行字段连接到一个字段中,并且我正在尝试使用LISTAGG,但是我需要在列表中的值是不同的。
我可以使用regexp_replace做几乎所有事情,但是当我为客户订购的订单太多时,我会在LISTAGG中遇到4000字符限制的问题。所以我试图在使用listagg之前区分值。子查询确实返回多个记录,我认为这是我想要的。我相信它可以工作,但是我得到ORA-01427: 单行子查询返回多行

任何帮助将不胜感激。

SELECT 
row_number() OVER(PARTITION BY custid ORDER BY order_date_key DESC) AS row_num
,r.custid
, listagg(
 (
  SELECT DISTINCT ppo_status
  FROM fact_order f1
   INNER JOIN orderheader c1 ON f1.order_guid = c1.order_guid
   INNER JOIN customer_profile r1 ON (c1.customer_guid = r1.source_custid)
  WHERE r1.custid = r.custid
 )
 , ',') WITHIN GROUP(ORDER BY ppo_status) OVER(PARTITION BY heat_custid) 
 AS list_ppo_status
) OVER(PARTITION BY custid) 
FROM fact_order fo
 INNER JOIN orderheader c ON fo.order_guid = c.order_guid
 INNER JOIN customer_profile r ON c.customer_guid = r.source_custid 
WHERE 1=1 
AND r.custid  IN (--'29299651', '31066429', '27399282', '31066537','0001637550')
;


专家解答

您不会将值列表传递到LISTAGG中,您允许LISTAGG操作resultset中的一组行。

例如-在这里,我尝试将员工姓名列表 “pipe” 到listagg的第一个参数中...

SQL> select
  2    d.deptno,
  3    listagg(
  4      ( select distinct ename from scott.emp where deptno = d.deptno ),',')
  5      within group (order by rowid)
  6  from  scott.dept d;
  d.deptno,
  *
ERROR at line 2:
ORA-00937: not a single-group group function


如果我需要按照上面的意图这样做,我将有一个标量选择,因为它将在一组行上循环,然后可以listagg

SQL> select
  2    d.deptno,
  3    ( select
  4         listagg(ename,',') within group ( order by empno )
  5      from scott.emp
  6      where deptno = d.deptno  ) as elist
  7  from  scott.dept d;

    DEPTNO ELIST
---------- ----------------------------------------------------------
        10 CLARK,KING,MILLER
        20 SMITH,JONES,SCOTT,ADAMS,FORD
        30 ALLEN,WARD,MARTIN,BLAKE,TURNER,JAMES
        40


「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论