postgreql:如何查询“大小写……否则什么都没有”

时间:2019-06-28 12:57:19

标签: postgresql

我有一个列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;

1 个答案:

答案 0 :(得分:1)

在案例表达式中将else添加为else 'zero',因此它将为不匹配的zeroPOS值返回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';