我有什么
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/