TableA (id int, match char(15), multiple char(10))
int match multiple
1 100
2 101
3 102
4 103
TableB (match char(15), match2 char(10))
match match2
100 ABC
100 NBG
101 NYF
102 NHW
102 UYQ
103 WOT
Now, I want to populate TableA.multiple = "YES" if in TableB for corresponding match, there exists more than one match2.
Expected result.
int match multiple
1 100 YES
2 101 NULL
3 102 YES
4 103 NULL
Thanks in advance !
My FAILED try:
Update A
SET multiple = 'YES'
From tableA A
Inner join tableB B ON A.match = B.match
WHERE (Select count(distinct(B.match2)) from TableB) > 2
Start with an extra-verbose version, just for its clarity:
With the HAVING clause, you can change this…
…to this:
So now we have:
I’m sure it can be made more compact, but I personally get confused by
UPDATEstatements containing non-obviousJOINclauses, especially in the middle of the night when I get the call that “the database isn’t working!”Don’t Make Me Think applies to coding, too.