ORA-00923: inconsistent datatypes: expected CHAR got NUMBER
场景:
使用case函数时数据类型报错;
解决:
使用的SQL如下
select case grouping_id(tsex)
when 1 then '合计'
else t.tsex
end as "tsex",
count(tsex)
from test_0128 t
group by grouping sets(tsex,null)
order by tsex;
原因是字段tsex为int类型,但因when 1then后定义的为字符类型,所以else后的类型需要跟then后的类型保持一致,比如可以修改为:
select case grouping_id(tsex)
when 1 then 999
else t.tsex
end as "tsex",
count(tsex)
from test_0128 t
group by grouping sets(tsex,null)
order by tsex;
只要then和else后的数据类型保持一致就不会报错,但这个时候并不能达到预期的效果,最后一行需要打印“合计”字符;
故此,可以将SQL修改为:
select case grouping_id(tsex)
when 1 then '合计'
else to_char(t.tsex)
end as "tsex",
count(tsex)
from test_0128 t
group by grouping sets(tsex,null)
order by tsex;
执行语句,成功执行,解决。