13

我在 11g 数据库上使用 SQL Developer 3.1.07 当我使用 listagg 从字段中提取多个值时,我在 listagg 列的结果中的每个字符之间得到一个空格。该查询返回我希望看到的所有值,只是多余的空格让我抓狂。有什么想法吗?

这是我使用过的一个查询,但每次我在查询中使用 listagg 时都会发生这种情况:

select a.personnum Emp_ID
      , a.personfullname Name
      , a.companyhiredtm Hire_Date
      , a.employmentstatus Status
      , a.employmentstatusdt Status_Date
      , h.Supervisor, h.Agency 
from vp_employeev42 a
  left outer join (select f.personid
                           , listagg (g.personcstmdatatxt, ',') within group  
                              (order by g.customdatadefid) Supervisor 
                    from vp_employeev42 f
                    left outer join personcstmdata g 
                        on f.personid = g.personid
                     where f.personnum like 'T%' 
                     and f.homelaborlevelnm3 = '1872'
                     and (g.customdatadefid = '1' 
                             or g.personcstmdatatxt is null)
                     group by f.personid) h
          on a.personid = h.personid
  left outer join (select f.personid
                      , listagg (g.personcstmdatatxt, ',') 
                              within group (order by g.customdatadefid) Agency
                   from vp_employeev42 f
                     left outer join personcstmdata g 
                           on f.personid = g.personid
                    where f.personnum like 'T%' 
                    and homelaborlevelnm3 = '1872' 
                    and (g.customdatadefid = '3' 
                              or g.personcstmdatatxt is null)
                      group by f.personid) h 
      on a.personid = h.personid   
where personnum like 'T%' 
and homelaborlevelnm3 = '1872' 
order by personnum;

这是我得到的结果:

EMP_ID,NAME,HIRE_DATE,STATUS,STATUS_DATE,SUPERVISOR,AGENCY
T98999,Lxxxxm, Lxxxn,20-SEP-12,Active,20-SEP-12,, S t a f f m a r k

T98989,Fxxxxn, Dxxxxa,10-DEC-12,Active,10-DEC-12,, S t a f f m a r k

T99989,Hxxxs, Cxxxxxa,02-OCT-12,Active,02-OCT-12,, S t a f f m a r k
T99999,Hxxxs, Dxxxn,30-JAN-12,Terminated,21-MAY-12, C x x x x x x x x x r   T x x x x r, P R O L O G I S T I X
4

3 回答 3

19

您是否有机会使用UTF-16+ NVARCHAR2?例如这个:

SQL> select * from nls_database_parameters where parameter='NLS_NCHAR_CHARACTERSET';

PARAMETER                      VALUE
------------------------------ ----------------------------------------
NLS_NCHAR_CHARACTERSET         AL16UTF16

SQL> drop table test;

Table dropped.

SQL> create table test(a nvarchar2(10));

Table created.

SQL> insert into test values ('test');

1 row created.

SQL> insert into test values ('test 2');

1 row created.

SQL> select listagg(a, ',') within group (order by 1) from test group by 1;

LISTAGG(A,',')WITHINGROUP(ORDERBY1)
--------------------------------------------------------------------------------
 t e s t, t e s t   2

你可以转换成一个字符来解决这个问题。如果这是不可接受的,您需要向 Oracle 支持提出请求。

SQL> select listagg(to_char(a),',') within group (order by 1) from test group by 1;

LISTAGG(TO_CHAR(A),',')WITHINGROUP(ORDERBY1)
--------------------------------------------------------------------------------
test,test 2

SQL>
于 2013-03-08T23:07:54.003 回答
5

这是目前没有修复的已知错误:

错误 13501087 11.2.0.3 RDBMS 11.2.0.3 SQL 执行 PRODID-5 PORTID-226

摘要: LISTAGG 返回奇怪的数据

*** 12/14/11 05:12 am *** (ADD: Impact/Symptom->WRONG RESULTS )

  SubComponent: SQL Analytics
  ===========================
  DETAILED PROBLEM DESCRIPTION
  ============================
  When using LISTAGG function  with NVARCHAR , data is returned with spaces
  between characters
于 2013-11-12T17:16:58.683 回答
0

在 Oracle 12c 上,我遇到了LISTAGG功能问题。我曾经 ASCIISTR()绕过它:

SELECT LISTAGG(TRIM(ASCIISTR(NVL(Name,''))), ';') 
WITHIN GROUP (ORDER BY NAME) AS Names 

在这里工作正常。

于 2019-09-07T18:47:11.647 回答