我有这个SQL查询:
select prefix, code, stat, complete, COUNT(*) as Count from table group by prefix, code, stat, complete order by prefix, code, stat, complete
列“前缀”是字母数字值(0-9a-zA-z)。我想要做的是,如果prefix的值是一个数字,则使该数字等于0。如果它是一个字母,它将保持其值。我试图在group by子句下添加以下行:
(case when prefix like '%[0-9]%' then 0 else prefix end)
但是我收到一个错误“将varchar值’J’转换为数据类型int时转换失败。”。
是什么导致此错误?如何获得“前缀”列以显示0或字母?
case when prefix like '%[0-9]%' then '0' else prefix end
您显然也需要此作为表达式GROUP BY:
GROUP BY
select NewPrefix = case when prefix like '%[0-9]%' then '0' else prefix end, code, stat, complete, COUNT(*) as Count from table group by case when prefix like '%[0-9]%' then '0' else prefix end, code, stat, complete order by case when prefix like '%[0-9]%' then '0' else prefix end, code, stat, complete