DB2 - 将行数据聚合到逗号列表

时间:2018-05-11 14:04:40

标签: sql db2

我正在读取一个db2表,其中数据存储为

user    group  
-----   ------
user1   groupa  
user1   groupb  
user1   groupc  
user2   groupc  
user3   groupc  

我想为每个用户返回一行。

user1 groupa,groupb,groupc

我已被其他表格告知,正确的方法是在

下面
SELECT VRAVW900_USER, SUBSTR(xmlserialize(xmlagg(xmltext(CONCAT( ', ',VRAVW900_Group))) as VARCHAR(1024)), 3) AS Groups
FROM VRADB2.VRAVW900 
GROUP BY VRAVW900_USER

这对我不起作用,我收到错误:

DB2 SQL Error: SQLCODE=-199, SQLSTATE=42601, SQLERRMC=VARCHAR;CLOB CHAR CHARACTER BLOB BINARY DBCLOB, DRIVER=3.64.133
    at com.ibm.db2.jcc.am.bd.a(bd.java:679)
    at com.ibm.db2.jcc.am.bd.a(bd.java:60)
    at com.ibm.db2.jcc.am.bd.a(bd.java:127)
    at com.ibm.db2.jcc.am.io.c(io.java:2706)
    at com.ibm.db2.jcc.am.io.d(io.java:2694)
    at com.ibm.db2.jcc.am.io.a(io.java:2143)
    at com.ibm.db2.jcc.am.io.a(io.java:2119)
    at com.ibm.db2.jcc.t4.ab.h(ab.java:136)
    at com.ibm.db2.jcc.t4.ab.b(ab.java:41)
    at com.ibm.db2.jcc.t4.p.a(p.java:32)
    at com.ibm.db2.jcc.t4.qb.i(qb.java:135)
    at com.ibm.db2.jcc.am.io.gb(io.java:2112)
    at com.ibm.db2.jcc.am.io.a(io.java:3191)
    at com.ibm.db2.jcc.am.io.a(io.java:686)
    at com.ibm.db2.jcc.am.io.executeQuery(io.java:669)
    at com.service_now.monitor.jdbc.JDBCRowSet.query(JDBCRowSet.java:64)
    at com.service_now.mid.probe.JDBCProbe.doSelect(JDBCProbe.java:312)
    at com.service_now.mid.probe.JDBCProbe.doQuery(JDBCProbe.java:198)
    at com.service_now.mid.probe.JDBCProbe.probe(JDBCProbe.java:123)
    at com.service_now.mid.probe.AProbe.process(AProbe.java:84)
    at com.service_now.mid.queue_worker.AWorker.runWorker(AWorker.java:125)
    at com.service_now.mid.queue_worker.AWorkerThread.run(AWorkerThread.java:20)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

我从ServiceNow,DB2 Version 11

运行它

1 个答案:

答案 0 :(得分:1)

VRAVW900_Group 的数据类型无效,这就是错误-199的原因。

然而,将VRAVW900_Group转换为VARCHAR并使用 LISTAGG 函数来聚合行数据。

SELECT VRAVW900_USER,
LISTAGG(Cast(VRAVW900_Group as VARCHAR), ', ') AS Groups
FROM VRADB2.VRAVW900 
GROUP BY VRAVW900_USER

检查此link是否使用 LISTAGG 功能。