postgresql - postgreql:如何查询“case when ... else nothing”
问题描述
我有一列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;
解决方案
将 case 表达式添加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';
推荐阅读
- typescript - 如何在 TypeScript 中将自定义界面与导入的界面合并?
- openstreetmap - 打开街道地图点击添加坐标到文本框
- python - 管道队列无法触发python函数
- java - 我希望将用户输入设置为等于列表中的相应数组?
- python - 如何在不使用 memoryviews 的情况下将 2d numpy 数组转换为 cython 中的指针数组
- java - java.lang.NoSuchFieldError: org.jboss.resteasy.core.providerfactory.ResteasyProviderFactoryImpl.initializeCommon 的实例
- python - 使用 numpy.distutils 管理命名空间
- python - 如何从文本文件中删除项目或字符串
- node.js - 如何在Nodejs中批量读取大csv文件?
- gradle - Gradle,如何从依赖项中排除资源