我需要检查每一行是否包含指定值

时间:2019-04-09 06:59:46

标签: sql postgresql

我有包含超过5K记录的六列。我要检查5K记录的六列中的每列是否存在指定的值。如果存在,则必须将其添加到相对于相应行的新列中

我已经尝试过将单个列作为ad6,但是我必须检查ad5ad4ad3是否包含ad6的值,即值将获得,或者ad5去获得它必须获得的值,否则ad4依此类推...

SELECT ad6,

CASE WHEN ad6 = 'NW' or ad6 = 'SW' or ad6 = 'SE'or ad6= 'NE' or ad6 = 'N' or ad6 = 'S' or ad6 = 'E' or ad6 ='W'or ad6 ='C'
THEN (ad6) 
ELSE null
END as sector

FROM public."Add_disp";

1 个答案:

答案 0 :(得分:0)

您应该能够像使用ad6一样继续使用其他情况。我尚未测试下面的语法,但这可以帮助您构建查询。

merge into some_table t
using (
    select some_row_id,
           case when ad6 = 'some value' then ad6
                when ad5 = 'some value' then ad5
                when ad4 = 'some value' then ad4
                when ad3 = 'some value' then ad3
                when ad2 = 'some value' then ad2
                when ad1 = 'some value' then ad1
                else null 
           end as sector
      from some_table where 1=1) AS v
ON t.some_row_id = v.some_row_id
WHEN MATCHED
    update SET t.some_col = v.sector