是的,一个case
声明将为此工作:
select (case when mycol in ('1', 'var1') then 'var1'
when mycol in ('2', 'var2') then 'var2'
when mycol in ('3', 'var3') then 'var3'
end),
sum(ttl) as Total
from mytable t
group by (case when mycol in ('1', 'var1') then 'var1'
when mycol in ('2', 'var2') then 'var2'
when mycol in ('3', 'var3') then 'var3'
end);
编辑:
如果您只有每个值包含多行的数据:
select (case when mycol in ('1', 'var1') then 'var1'
when mycol in ('2', 'var2') then 'var2'
when mycol in ('3', 'var3') then 'var3'
end),
count(*) as Total
from mytable t
group by (case when mycol in ('1', 'var1') then 'var1'
when mycol in ('2', 'var2') then 'var2'
when mycol in ('3', 'var3') then 'var3'
end);