这有效:
SELECT (CASE
WHEN x = 'value' THEN
a.col1
ELSE
nvl(a.col1, a.col2)
END)
FROM table1 a
WHERE a.this = 'that'
GROUP BY (CASE
WHEN x = 'value' THEN
a.col1
ELSE
nvl(a.col1, a.col2)
END)
但是尝试让 case 语句执行 IN 语句(在此处尝试更动态的 sql),以下代码会导致ORA-00979错误。
SELECT (CASE
WHEN x IN (SELECT me FROM here WHERE this = 'example') THEN
a.col1
ELSE
nvl(a.col1, a.col2)
END)
FROM table1 a
WHERE a.this = 'that'
GROUP BY (CASE
WHEN x IN (SELECT me FROM here WHERE this = 'example') THEN
a.col1
ELSE
nvl(a.col1, a.col2)
END)
是否有可能使这项工作或有替代方案?谢谢。——乔纳斯
Benoit:这是根据您的 sql 重新创建错误的修改后的 sql:
select (case when a.y IN (select 'A'||ROWNUM from dual where rownum=1) then 1 else 0 end)
from (SELECT 'A'||ROWNUM y, 'B' x FROM DUAL CONNECT BY ROWNUM <= 3) a where x = 'B'
group by (case when a.y IN (select 'A'||ROWNUM from dual where rownum=1) then 1 else 0 end)
;
基本上缺少的是 FROM 表应该有多个值,并且在 CASE 语句中引用了一个列。