Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 19g之前的不同LISTAGG_Sql_String_Oracle_Distinct_Aggregate Functions - Fatal编程技术网

Sql 19g之前的不同LISTAGG

Sql 19g之前的不同LISTAGG,sql,string,oracle,distinct,aggregate-functions,Sql,String,Oracle,Distinct,Aggregate Functions,我有一个查询,当我使用LISTAGG时,它会给我重复的结果,因为我的Oracle版本是18.4,我不能将select DISTINCT与LISTAGG一起使用,所以我使用的是嵌套的select,但似乎无法让它工作。你能告诉我我错过了什么吗? 这是一个有效但会返回重复结果的查询: 例如,actionitem_id 1将显示“abc、abc、abc、def、def、def”的修改列 我希望actionitem_id 1显示“abc,def”的修改列 因此,我尝试运行以下查询,但出现以下错误: ORA

我有一个查询,当我使用LISTAGG时,它会给我重复的结果,因为我的Oracle版本是18.4,我不能将select DISTINCT与LISTAGG一起使用,所以我使用的是嵌套的select,但似乎无法让它工作。你能告诉我我错过了什么吗?
这是一个有效但会返回重复结果的查询: 例如,actionitem_id 1将显示“abc、abc、abc、def、def、def”的修改列

我希望actionitem_id 1显示“abc,def”的修改列 因此,我尝试运行以下查询,但出现以下错误: ORA-00904:“PAT”。“PROPERTYNAME”:无效标识符 90400000-%s:无效标识符

你能告诉我怎么解决这个问题吗? 谢谢这里有一个方法:

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
action/property元组,然后按操作进行聚合来实现的

注:

  • 使用标准连接!隐式连接(在
    from
    子句中使用逗号,在
    where
    子句中使用连接条件)是旧语法,不应在新代码中使用。我相应地修改了查询

  • 尽可能使用日期文字(
    date'yyyyy-MM-DD'
    )而不是
    to_date()
    :这是标准SQL。。。而且比打字要短


加入
<代码>加入<代码>加入。这是自1990年代以来的标准SQL语法。在LISTAGG中使用表shortname
pat
,但此表shortname未在外部查询中定义。您好,我收到以下错误消息:ORA-00904:“pat”。“PROPERTYNAME”:无效标识符00904。00000-“%s:无效标识符”*原因:*操作:第2行第58列出错内部查询工作,但当我运行完整查询时,我发现了这个错误,我将“)”后面的“t”改为“pat”,现在正在工作!谢谢GMB,鲍勃和戈登。。。很抱歉连接语法,我只是需要习惯新的方式。。。谢谢你的帮助@seth:甚至不需要在外部查询中为列添加前缀。我修复了查询。谢谢,这也起了作用,但不确定它如何知道引用的是哪个表actionitem_id和propertyname,我猜它会查看内部查询?
    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
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