带有CASE的Oracle Sql LISTAGG

时间:2017-12-29 12:23:23

标签: oracle11g listagg

我正在尝试将LISTAGG函数添加到以下查询中,以便输出可以是单个值,以逗号分隔。有人建议如何向其添加LISTAGG函数。

SELECT CASE A.ATTRIBUTE_NAME
WHEN 'EA' THEN 'EMAIL ADDRESS'
WHEN 'CP' THEN 'Company'
WHEN 'OG' THEN 'Organization'
END AS USER_DETAILS
FROM USERS A LEFT OUTER JOIN DETAILS B
ON A.ID = B.USER_ID
WHERE USER_NAME LIKE '%John%'

1 个答案:

答案 0 :(得分:0)

根据您的描述,您只想将案例表达式放在listagg()调用中:

SELECT LISTAGG(
  CASE A.ATTRIBUTE_NAME
    WHEN 'EA' THEN 'EMAIL ADDRESS'
    WHEN 'CP' THEN 'Company'
    WHEN 'OG' THEN 'Organization'
  END,
  ',') WITHIN GROUP (ORDER BY A.ATTRIBUTE_NAME) AS USER_DETAILS
FROM USERS A LEFT OUTER JOIN DETAILS B
ON A.ID = B.USER_ID
WHERE USER_NAME LIKE '%John%'

虽然它看起来应该是指B.ATTRIBUTE_NAME;也许你错了修改发布的代码。您可能希望以不同方式对列表中的项目进行排序。