gpt4 book ai didi

oracle - 在 CASE 语句的 "ELSE"中使用 select 给出 ORA-00937 : not a single-group group function

转载 作者:行者123 更新时间:2023-12-02 11:33:24 25 4
gpt4 key购买 nike

当我尝试在查询的 ELSE 部分中使用 listagg 进行选择语句时,出现 ORA-00937 错误。有办法解决这个问题吗?

select 
CASE
when count(*) = 0 then
'There are no users connected'
else
(select
listagg(osuser, ', ') within group (order by osuser)
from (select distinct osuser from v$session)
where osuser != 'SYSTEM' and osuser not like 'VMCONFTEST%')
end
from v$session
where username is not null
and osuser not like 'VMCONFTEST%';

即使我用更简单的内容替换 select 语句,例如 select osuser from v$session 我也会收到相同的错误。

最佳答案

采取了稍微不同的方法,但似乎有效。无需进行大小写和计数,只需检查聚合是否为空(合并返回一系列中的第一个非空值)以及它是否替换了您的消息。这避免了我认为不需要的二级分组。

太糟糕了,listagg 也不支持聚合内的不同;我们可以避免内联 View 。

SELECT coalesce(listagg(A.osuser, ', ') within group (order by A.osuser), 
'There are no users connected') as userList
FROM (select distinct osuser from v$session) A
WHERE A.osuser!= 'SYSTEM' and A.osuser not like 'VMCONFTEST%'

这确实有开销,因为它尝试生成您的 case 语句可能试图短路的用户列表。但是,如果 V$session 中没有记录,则选择不同的应该很快。

尽管说实话,我不确定为什么我们需要这样做。列表中的空通常是适当的响应,表明没有用户。并且 UI 将处理 null,这意味着没有用户。

如果我们将 where 子句添加到内联 View 中,甚至可能会更快。

SELECT coalesce(listagg(A.osuser, ', ') within group (order by A.osuser), 
'There are no users connected') as userList
FROM (SELECT distinct osuser
FROM v$session
WHERE A.osuser!= 'SYSTEM'
and A.osuser not like 'VMCONFTEST%') A

关于oracle - 在 CASE 语句的 "ELSE"中使用 select 给出 ORA-00937 : not a single-group group function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35180792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com