使用LISTAGG

时间:2019-10-09 06:17:30

标签: sql oracle oracle11g

我的查询正在使用LISTAGG,并且显示错误00937。00000-“不是单组分组函数” 我经历了很多类似于此错误的stackoverflow问题,这些答案正在使用一些子查询来解决问题。我不想在此查询中编写子查询

SELECT DISTINCT AMD.UNID AS APPLICATION_REF_ID, 
LISTAGG(LOC.PLT,',') WITHIN GROUP (ORDER BY LOC.PLT ) AS MYLOC  
FROM TAB1 AMD
LEFT JOIN TAB2 PER
ON NVL (AMD.PERMITNEWID, AMD.PERMITID) = PER.UNID
LEFT JOIN TAB3 LOC
ON NVL(AMD.PERMITNEWID, AMD.PERMITID) = LOC.PERMITID
WHERE NVL (AMD.PERMITNEWID, AMD.PERMITID) = PER.UNID

2 个答案:

答案 0 :(得分:1)

尝试分组依据:

SELECT amd.unid AS application_ref_id,
       LISTAGG (loc.plt, ',') WITHIN GROUP (ORDER BY loc.plt) AS myloc
  FROM tab1 amd
       LEFT JOIN tab2 per ON NVL (amd.permitnewid, amd.permitid) = per.unid
       LEFT JOIN tab3 loc
           ON NVL (amd.permitnewid, amd.permitid) = loc.permitid
 WHERE NVL (amd.permitnewid, amd.permitid) = per.unid
GROUP BY amd.unid

答案 1 :(得分:1)

您缺少group by,如果您要按distinct()分组,则不需要UNID

SELECT AMD.UNID AS APPLICATION_REF_ID, 
LISTAGG(LOC.PLT,',') WITHIN GROUP (ORDER BY LOC.PLT ) AS MYLOC  
FROM TAB1 AMD
LEFT JOIN TAB2 PER
ON NVL (AMD.PERMITNEWID, AMD.PERMITID) = PER.UNID
LEFT JOIN TAB3 LOC
ON NVL(AMD.PERMITNEWID, AMD.PERMITID) = LOC.PERMITID
WHERE NVL (AMD.PERMITNEWID, AMD.PERMITID) = PER.UNID
GROUP BY AMD.UNID