简体   繁体   中英

SQL Server: Updating a column

I have a TblA

ID Match Code Status
1   001   A    
2   001   B   
3   002   A
4   003   A
5   003   V
6   004   A
7   004   B

I want to populate Status with 'FAIL' according to : Code "A" and "B" should both exist for every match number. For 001,002,003 both A, B should exist. if not, FAIL the whole Match. Expected table:

ID Match Code Status
1   001   A    NULL
2   001   B    NULL
3   002   A    FAIL
4   003   A    FAIL
5   003   V    FAIL
6   004   A    NULL
7   004   B    NULL

Thanks !

Here you go:

update [TblA]
set [Status] = 'FAIL' where 
Match NOT in
(select match from tblA where Code = 'A'
intersect
select match from tblA where Code = 'B');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM