[英]Redshift SQL: Check if same set of values are IN and NOT IN two different columns of table
[英]Checking the same values in two different columns of same table and then compare in sql
cid ctypeid tid check asscid ci
19149 6 2 0 NULL 0
253440 1 1 0 22297922 1
1361285 5 2 0 NULL 1
22297922 2 1 1 NULL NULL
49821961 5 1 0 NULL 1
我必须检查asscid
即22297922
是否在实际存在的cid列中。 所以我必须比较asscid
在cid中的时间,然后在asscid
(其中cid
为253440
)的情况下获得ci
的值(在这种情况下为1),然后将ci
的相同值1分配给cid
列22297922
,即在这种情况下为null
。
通过自我联接使用更新。
选择:
SELECT t.asscid, t2.cid, t.ci, t2.ci --We will next update t2.ci with t.ci
FROM table t
JOIN table t2 on t.asscid = t2.cid
更新:
UPDATE t2
SET t2.ci = t.ci
FROM table t
JOIN table t2 on t.asscid = t2.cid
使用条件更新:(以便仅更新具有不同ci
行)
UPDATE t2
SET t2.ci = t.ci
FROM table t
JOIN table t2 on t.asscid = t2.cid
and t.ci <> t2.ci
从emp A中选择Assid(如果存在)(从emp B中选择1,其中a.assid = b.Cis)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.