简体   繁体   English

DB2 - 带有 DISTINCT 子句的 LISTAGG() - 不起作用?

[英]DB2 - LISTAGG() with DISTINCT clause - doesn't work?

My query has a column with a small number of values in it, and I need to display them in a single field for each grouped result set - eg if I had an employee in 3 different departments, I'd want to see something like我的查询有一列包含少量值,我需要将它们显示在每个分组结果集的单个字段中 - 例如,如果我在 3 个不同部门有一名员工,我希望看到类似的内容

EMPID          DEPTS                                   SOMENUMBER  SOMEOTHERNUMBER
------         ------                                  ----------- ---------------
0001           ACCOUNTING, CUST SERVICE, CALL CENTER        100           200

The problem is when there are multiple duplicate departments for the employee.问题是当员工有多个重复的部门时。 I see numerous questions on how to figure this out for Oracle and other DMBSs, but nothing specific to DB2.我看到很多关于如何为 Oracle 和其他 DMBS 解决这个问题的问题,但没有任何特定于 DB2 的问题。 IBM's own documentation at https://www.ibm.com/support/knowledgecenter/SSFMBX/com.ibm.swg.im.dashdb.sql.ref.doc/doc/r0058709.html says: - "If DISTINCT is specified, duplicate string-expression values are eliminated.", and - "If DISTINCT is specified for LISTAGG, the sort-key of the ORDER BY specification must match string-expression (SQLSTATE 42822). If string-expression is implicitly cast, the sort-key must explicitly include a corresponding matching cast specification.". IBM 在https://www.ibm.com/support/knowledgecenter/SSFMBX/com.ibm.swg.im.dashdb.sql.ref.doc/doc/r0058709.html上的文档说:-“如果指定了 DISTINCT,重复的字符串表达式值被消除。”和-“如果为 LISTAGG 指定了 DISTINCT,则 ORDER BY 规范的排序键必须匹配字符串表达式 (SQLSTATE 42822)。如果字符串表达式被隐式转换,则排序-键必须明确包含相应的匹配转换规范。”。

As a simplistic example:作为一个简单的例子:

with mylist (field1) as 
    ( values 'A','A','B','C','D','A','C'
    )
    select listagg (distinct field1, ', ') 
        within group (order by field1) 
        from mylist;

The info on the IBM page suggests that this should return 'A, B, C, D' but instead I get 'AAA B, C. C, D'. IBM 页面上的信息表明这应该返回“A、B、C、D”,但我得到的是“AAA B、C、C、D”。

I should be able to get around this by having a subquery do some initial rolling up - eg我应该能够通过让子查询做一些初始汇总来解决这个问题 - 例如

SELECT EMPLOYEE, LISTAGG(DEPARTMENT,', '),  
    SUM(SOMENUMBER) SOMENUMBER, SUM(SOMEOTHERNUMBER) SOMEOTHERNUMBER
from (
    SELECT EMPLOYEE, DEPARTMENT, SUM(SOMENUMBER) SOMENUMBER, SUM(SOMEOTHERNUMBER) SOMEOTHERNUMBER
    FROM EMPLOYEES GROUP BY EMPLOYEE, DEPARTMENT)
) GROUP BY EMPLOYEE

and in fact I guess that's what I'll do, but the IBM documentation sure suggests the DISTINCT ought to do the trick.事实上,我想这就是我要做的,但是 IBM 文档确实表明 DISTINCT 应​​该可以解决问题。 What am I missing?我错过了什么?

You haven't specified, but if you use DB2 9.7 LUW, like me, LISTAGG(DISTINCT .. doesn't (yet) work. You have to workaround it by XML functions, eg:您尚未指定,但如果您像我一样使用 DB2 9.7 LUW,则LISTAGG(DISTINCT ..还没有) 工作。您必须通过 XML 函数来解决它,例如:

with mylist (field1) as 
    ( values 'A','A','B','C','D','A','C'
    )
    select XMLCAST(
             XMLQUERY('string-join(distinct-values($x//row), ", ")'
                      PASSING XMLGROUP(field1 ORDER BY field1) AS "x"
           ) AS VARCHAR(200))
        from mylist

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

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