declare @s varchar2(4000);@s := 'select ...';if g = '0' then @s := @s||' group by a'; end if;if g = '1' then @s := @s||' group by b'; end if;if g = '2' then @s := @s||' group by a,b'; end if;if g = '3' then @s := @s||' group by c'; end if;open cur for @s;
对这个例子来说 group 后的case 需要写两个 并且由于分组的对象不确定,所有查询字段都需要聚合 例如 select max(a),max(b),max(c),max(g),count(1) from test group by case g when 0 then a when 1 then b when 2 then a when 3 then c end, case g when 2 then b end;
@s := @s||' group by a';
end if;if g = '1' then
@s := @s||' group by b';
end if;if g = '2' then
@s := @s||' group by a,b';
end if;if g = '3' then
@s := @s||' group by c';
end if;open cur for @s;
group 后的case 需要写两个
并且由于分组的对象不确定,所有查询字段都需要聚合
例如
select max(a),max(b),max(c),max(g),count(1)
from test
group by case g when 0 then a when 1 then b when 2 then a when 3 then c end,
case g when 2 then b end;
或者你case后用了when g='2' then a,b ?
你那么写会有语法错误
select sum(case G when '1' then colUSD when '2' then colCNY end) 应收数
from tablename
group by case G when '1' then colA when '2' then colB end
case G when '2' then colC end