sql - 根据 PostgreSQL 中的条件将值插入到新的数组列

标签 sql postgresql greenplum

我有什么

  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}

最佳答案

您可以根据如下所示的 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

关于sql - 根据 PostgreSQL 中的条件将值插入到新的数组列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50958273/

相关文章:

hadoop - 来自 Ambari 的 HAWQ 服务检查失败

sql - 计算 pandas 数据框中的不同值

SQL 查询检查第一个表中的列或查找第二个表中的行

sql - 当不需要第二个表中的数据时,JOIN 的效率是否比 EXISTS IN 更高/更低?

sql - 为什么我不能在 postgres 中用双引号引用我的序列?

postgresql - 为什么 PostgreSQL 性能在 VACUUM FULL 之后没有恢复到最大值?

hadoop - 错误 : protocol "gphdfs" does not exist

mysql - 连接 3 个表以获得单行

python - 邮政数据库。 models.py 没有转化为数据库方案

sql - 如何使用 SQL 窗口函数计算聚合的百分比