1

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 !

1 Answer 1

4

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');
Sign up to request clarification or add additional context in comments.

4 Comments

If there are only a small number of additional codes like v it is acceptable to add additional intersects in the subquery one after the other.
I may have edited over someone's comment...apologies for that...still an overflow noob.
+1 nice and simple. I would have somehow aggregated the whole thing and made it more complex than it needed to be.
Thanks Lieven, An aggregated subquery was the first thing to pop into my head as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.