我有一列 category
并且有 3 个可能的值“pos”、“neg”和“zero”。
我正在寻找一个查询,当它为 pos 时显示 POSITIVE,当它为 neg 时显示 NEGATIVE,当它为零时则什么也没有。
在下面的查询中,当它为零时我得到 null。有什么办法可以不选零吗?
SELECT distinct(category),
case when nature='POS' then 'POSITIVE'
when nature='NEG' then 'NEGATIVE'
end as category_rm
FROM table_h;
最佳答案
添加else
在 case 表达式中为 else 'zero'
, 所以它会返回 zero
对于不匹配的 POS
和 NEG
值(value)观。
SELECT distinct category,
case when nature = 'POS' then 'POSITIVE'
when nature = 'NEG' then 'NEGATIVE'
else 'zero'
end as category_rm
FROM table_h;
Is there any way to not select zero?
如果是这样,请避免在 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';
关于postgresql - postgresql : how to do query "case when ... else nothing",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56807401/