【问题标题】:Insert values to new array column based on conditions in PostgreSQL根据 PostgreSQL 中的条件将值插入新数组列
【发布时间】:2018-11-30 04:20:00
【问题描述】:

我有什么

  id  test_1   test_2  test_3        Indicator_column
  1     651       40     0.4      {test_1,test_2,test_3}
  1     625       80     0.6      {test_1,test_2,test_3}
  1     510       60     0.78     {test_1,test_2,test_3}
  1     710       90     0.4      {test_1,test_2,test_3}
  1     550       Null   0.2      {test_1,test_2,test_3}

我需要什么:

我在表格和 excel 中都有这些条件

1) 如果测试 1 的值介于 650-800 之间,则为 1,否则为 0

2) 如果测试 2 的值大于 80,则为 1,否则为 0

3) 如果测试 3 的值大于 0.5,则为 1,否则为 0

    id  test_1   test_2  test_3        Indicator_column        exclude_flag
     1     651       40     0.4      {test_1,test_2,test_3}      {1,0,0}
     1     625       80    0.6       {test_1,test_2,test_3}      {0,0,1}
     1     510       60     0.78     {test_1,test_2,test_3}      {0,0,1}
     1     710       90     0.4      {test_1,test_2,test_3}      {1,1,0}
     1     550       Null   0.2      {test_1,test_2,test_3}      {0,0,0}

【问题讨论】:

    标签: sql postgresql greenplum


    【解决方案1】:

    您可以使用基于CASE 条件的元素构造一个ARRAY[],如下所示:

    select
      id, test_1, test_2, test_3, indicator_column,
      ARRAY[
        case when test_1 between 650 and 800 then 1 else 0 end,
        case when test_2 > 80 then 1 else 0 end,
        case when test_3 > 0.5 then 1 else 0 end,
      ] as exclude_flag
    from t
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 2019-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多