我的查询返回以下结果,每当我看到COLUMN4值的值为“YES”时,我需要为COLUMN1组下的所有记录设置“YES”值。 例如,DAVID有2条记录,NO和YES-但我的目标状态对所有行都应为“YES”,因为对于至少一条记录,它的值为“YES”。
查询结果
Column1 Column2 Column3 Column4
=================================
Mary AA AAA YES
Mary BB BBB YES
David AA AAA YES
David BB BBB NO
Clara AA AAA NO
Clara BB BBB NO
请求的目标国家
Column1 Column2 Column3 Column4
================================================
Mary AA AAA YES
Mary BB BBB YES
David AA AAA YES
David BB BBB **YES**
Clara AA AAA NO
Clara BB BBB NO
答案 0 :(得分:2)
UPDATE my_table m set m.column4='YES'
WHERE m.column4='NO'
AND exists(
select 1 from my_table mm where mm.column1= m.column1 and mm.column4 = 'YES')
答案 1 :(得分:1)
由于评论:
UPDATE my_table m1 set m.column4='YES'
WHERE m1.column4='NO'
AND m1.column1 IN (
select m2.column1 from my_table m2 where mm.column4 = 'YES')
答案 2 :(得分:1)
这是您想要的Select语句,
111.111.111.111 www.yourdomain.com
结果:
SELECT mt.Column1,mt.Column2,mt.Column3,
(CASE WHEN mt2.cc IS NULL THEN 'NO' ELSE 'YES' END) AS Column4
FROM mytable mt LEFT JOIN
(SELECT Column1,COUNT(*) AS cc FROM mytable WHERE Column4 = 'YES' GROUP BY Column1)
AS mt2 ON mt2.Column1 = mt.Column1