PostgreSQL CASE … END具有多个条件

前端之家收集整理的这篇文章主要介绍了PostgreSQL CASE … END具有多个条件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的表的摘录:
gid    |    datepose    |    pvc
---------+----------------+------------
 1       |  1961          | 01
 2       |  1949          |
 3       |  1990          | 02
 1       |  1981          |
 1       |                | 03
 1       |                |

我想使用SELECT CASE填写PVC列,如下所示:

SELECT

 gid,CASE
  WHEN (pvc IS NULL OR pvc = '') AND datpose < 1980) THEN '01'
  WHEN (pvc IS NULL OR pvc = '') AND datpose >= 1980) THEN '02'
  WHEN (pvc IS NULL OR pvc = '') AND (datpose IS NULL OR datpose = 0) THEN '03'
 END AS pvc

FROM my_table ;

结果是与源表相同的内容,没有发生任何事情,我在pg_log文件中没有收到错误信息。这可能是一个语法错误,或在WHEN子句中使用多个条件的问题?

感谢您的帮助和建议!

这种代码也许应该适合你
SELECT
 *,CASE
  WHEN (pvc IS NULL OR pvc = '') AND (datepose < 1980) THEN '01'
  WHEN (pvc IS NULL OR pvc = '') AND (datepose >= 1980) THEN '02'
  WHEN (pvc IS NULL OR pvc = '') AND (datepose IS NULL OR datepose = 0) THEN '03'
  ELSE '00'
 END AS modifiedpvc
FROM my_table;


 gid | datepose | pvc | modifiedpvc 
-----+----------+-----+-------------
   1 |     1961 | 01  | 00
   2 |     1949 |     | 01
   3 |     1990 | 02  | 00
   1 |     1981 |     | 02
   1 |          | 03  | 00
   1 |          |     | 03
(6 rows)
原文链接:https://www.f2er.com/postgresql/192971.html

猜你在找的Postgre SQL相关文章