This works:
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)
But trying to have the case statement do an IN statement(trying for a more dynamic sql here), the following code results to an ORA-00979 error.
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)
Is it possible to make this work or have an alternative? Thanks. --Jonas
Benoit: Here's a modified sql based on your sql that recreates the error:
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)
;
Basically what was missing was that the FROM table should have more than one values and that a column was referenced in the CASE statement.