当字段G = '0'时,group by a
当字段G = '1'时,group by b
当字段G = '2'时,group by a,b
当字段G = '3'时,group by c用case when好像不可以,
应该怎么实现?
当字段G = '1'时,group by b
当字段G = '2'时,group by a,b
当字段G = '3'时,group by c用case when好像不可以,
应该怎么实现?
@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