gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:02:18 25 4
gpt4 key购买 nike

我有什么

  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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com