【问题标题】:Distinct LISTAGG prior to 19g [duplicate]19g 之前的不同 LISTAGG [重复]
【发布时间】:2020-11-06 13:41:20
【问题描述】:

当我使用 LISTAGG 时,我有一个查询会给出重复的结果,因为我的 Oracle 版本是 18.4,所以我不能将 select DISTINCT 与 LISTAGG 一起使用,所以我使用的是嵌套选择,但似乎无法让它工作。你能告诉我我错过了什么吗?
这是有效但带回重复结果的查询: 例如,actionitem_id 1 将显示 columnmodified of "abc,abc,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

你能告诉我如何解决这个问题吗? 谢谢

【问题讨论】:

  • JOINJOINJOIN。这是自 1990 年代以来的标准 SQL 语法。

标签: sql string oracle distinct aggregate-functions


【解决方案1】:

这是一种方法:

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():这是标准 SQL...并且键入起来更短

【讨论】:

  • 在 LISTAGG 中,您使用表短名称 pat,但此表短名称未在外部查询中定义。
  • 嗨,我收到以下错误消息:ORA-00904: "PAT"."PROPERTYNAME": invalid identifier 00904. 00000 - "%s: invalid identifier" *原因:*操作:错误在 Line: 2 Column: 58 内部查询有效,但是当我运行完整查询时出现此错误
  • 我明白了,我把“)”后面的“t”改成了“pat”,现在可以工作了!感谢 GMB、Bob 和 Gordon... 对连接语法感到抱歉,我只需要习惯新的事物方式...感谢您的帮助!
  • @seth:甚至不需要在外部查询中为列添加前缀。我修复了查询。
  • 谢谢,这也有效,但不确定它如何知道它引用了哪个表 actionitem_id 和 propertyname,我猜它会查看内部查询?
猜你喜欢
  • 1970-01-01
  • 2021-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 2015-04-02
  • 2021-11-27
  • 1970-01-01
相关资源
最近更新 更多