【问题标题】:Create a new column based on values of other columns in postgres根据 postgres 中其他列的值创建一个新列
【发布时间】:2018-05-23 13:51:09
【问题描述】:

我有什么

customer_id
   1
   2 
   2
   1
   3

我想要什么(对于所有带有 1 的客户 ID,我想在新列“warining_customer_id”中标记为有效,其余客户 ID 为无效

customer_id    warining_customer_id
   1             Valid
   2             Invalid
   2             Invalid
   1             Valid
   3             Invalid

【问题讨论】:

  • 提示:CASE 表达式。
  • 使用update table1 set warining_customer_id = ...

标签: sql postgresql greenplum


【解决方案1】:

这里是一个例子

SELECT   customer.customer_id AS customer_id,
         CASE WHEN customer.customer_id = 1 THEN 'Valid' ELSE 'Invalid' END
             AS warning_customer_id
  FROM   (SELECT   1 AS customer_id FROM DUAL
          UNION ALL
          SELECT   2 AS customer_id FROM DUAL) customer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2021-10-15
    • 1970-01-01
    相关资源
    最近更新 更多