当我使用LISTAGG时,我有一个查询,它给了我重复的结果,因为我的Oracle版本是18.4,我不能使用带有LISTAGG的select DISTINCT,所以我使用嵌套select,但似乎不能让它工作。你能让我知道我错过了什么吗?
这是可以工作但返回重复结果的查询:例如,actionitem_id 1将显示"abc,def,def,def“的列修改
SELECT
ai.actionitem_id,
LISTAGG( pat.PROPERTYNAME,',')
WITHIN GROUP(ORDER BY pat.PROPERTYNAME) AS columnmodified
FROM propertydefs pd , property_audit_trail pat, rt_actionitem ai
where pd.bundlename = 'ActionItem'
and pd.name in ('Action Item Revised Target Date','PVrfy')
and pd.propertydefid = pat.propertydefid
and pat.modifydate > to_date('11/01/2020', 'MM/DD/YYYY')
and pat.resourceid = ai.ACTIONITEM_ID
GROUP BY
ai.actionitem_id
ORDER BY
ai.actionitem_id
我希望actionitem_id 1显示"abc,def“的列修改,所以我试图运行以下查询,但得到以下错误: ORA- 00904:”PAT“。”PROPERTYNAME“:无效标识符00904。00000 - "%s:无效的标识符“
SELECT
ai.actionitem_id,
(select LISTAGG( pat.PROPERTYNAME,',')
WITHIN GROUP(ORDER BY pat.PROPERTYNAME) AS columnmodified
FROM (select unique pat.PROPERTYNAME
from propertydefs pd , property_audit_trail pat, rt_actionitem ai
where pd.bundlename = 'ActionItem'
and pd.name in ('Action Item Revised Target Date','PVrfy')
and pd.propertydefid = pat.propertydefid
and pat.modifydate > to_date('11/01/2020', 'MM/DD/YYYY')
and pat.resourceid = ai.ACTIONITEM_ID
GROUP BY
ai.actionitem_id
ORDER BY
ai.actionitem_id)) PROPERTYNAME
from rt_actionitem ai
你能告诉我怎么解决这个问题吗?谢谢
发布于 2020-11-06 21:48:39
以下是一种方法:
select actionitem_id,
listagg(propertyname,',') within group(order by propertyname) as columnmodified
from (
select distinct ai.actionitem_id, pat.propertyname
from propertydefs pd
inner join property_audit_trail pat on pat.propertydefid = pd.propertydefid
inner join rt_actionitem ai on ai.actionitem_id = pat.resourceid
where
pd.bundlename = 'actionitem'
and pd.name in ('action item revised target date','pvrfy')
and pat.modifydate > date '2020-11-01'
) t
group by actionitem_id
order by actionitem_id
这是通过首先选择子查询中的distinct
操作/属性元组,然后按操作聚合来实现的。
备注:
from
子句中使用逗号,在where
子句中使用连接条件)是遗留语法,不应在新代码中使用。我相应地修改了查询尽可能使用日期字面量(date 'YYYY-MM-DD'
)而不是to_date()
:这是标准...,并且比键入更短
https://stackoverflow.com/questions/64715723
复制相似问题