具有不同计数的案例列表

问题描述 投票:0回答:1

我希望需要以下查询才能工作:

select tbl.depno
,  case
                 when count(distinct ntl.number) over(partition by tbl.id) > 1 
                 then listagg(ntl.number, ',') within group (order by ntl.number) 
              
                 else 'test'
                end as multiple_number

from table tbl,
notable ntl
where tbl.id = ntl.id
group by tbl.depno

这样就可以了:

select tbl.depno
, listagg(ntl.number, ',') within group (order by ntl.number)

from table tbl,
notable ntl
where tbl.id = ntl.id
group by tbl.depno

但是我需要它在我的情况下工作,当实际上有更多数字时,然后聚合,如果只有一个,那么它必须插入文本(现在,测试)

所以当我运行第一部分时收到的错误消息是:

ORA--00979: not a GROUP BY expression
sql oracle plsql
1个回答
0
投票
SELECT 
    tbl.depno,
    CASE
        WHEN count(distinct ntl.number) over(partition by tbl.id) > 1 
        THEN listagg(ntl.number, ',') WITHIN GROUP (ORDER BY ntl.number) 
        ELSE 'test'
    END AS multiple_number
FROM 
    table tbl
JOIN 
    notable ntl ON tbl.id = ntl.id
GROUP BY 
    tbl.depno, tbl.id;
© www.soinside.com 2019 - 2024. All rights reserved.