我有一个列category
,并且可能有3个值“ pos”,“ neg”和“零”。
我正在寻找一个查询,该查询在正数时显示POSITIVE,在负数时显示负数,在零时什么都不显示。
在下面的查询中,零时为空。有什么办法可以不选择零?
SELECT distinct(category),
case when nature='POS' then 'POSITIVE'
when nature='NEG' then 'NEGATIVE'
end as category_rm
FROM table_h;
答案 0 :(得分:1)
在案例表达式中将else
添加为else 'zero'
,因此它将为不匹配的zero
和POS
值返回NEG
。
SELECT distinct category,
case when nature = 'POS' then 'POSITIVE'
when nature = 'NEG' then 'NEGATIVE'
else 'zero'
end as category_rm
FROM table_h;
有什么办法可以不选择零?
如果这样,请在WHERE
子句中将其避免为
SELECT distinct category,
case when nature = 'POS' then 'POSITIVE'
when nature = 'NEG' then 'NEGATIVE'
end as category_rm
FROM table_h
WHERE nature != 'zero';