我有两列:LABEL_ID
和TYPE
。
LABEL_ID TYPE
1234 Salad
1234 Dressing
4455 Croutons
5889 Salad
6574 Nuts
8845 Bacon Bits
8845 Salad
我要做的是,如果一个LABEL_ID
具有特定的TYPE
(=沙拉),则设置具有相同值的第三个指标列。如果Type = Salad
,则为这两个记录设置IND = YES
。
LABEL_ID TYPE IND
1234 Salad YES
1234 Dressing YES
4455 Croutons NO
5889 SALAD YES
6574 Nuts NO
8845 Bacon Bits YES
8845 Salad YES
答案 0 :(得分:2)
这应该适合你。
SELECT a.*,
CASE WHEN EXISTS (SELECT LABEL_ID FROM Table b
WHERE a.Label_id = b.Label_id AND b.Type = 'Salad') THEN 'YES'
ELSE 'NO'
END [IND]
FROM Table a
答案 1 :(得分:2)
UPDATE t
SET
IND = 'YES'
FROM your_table as t
INNER JOIN your_table as p on t.LABEL_ID = p.LABEL_ID
WHERE p.TYPE = 'Salad'