My table structures are shown here:
Table1
| Col1 |
|---|
| 10 |
| 20 |
| 20 |
| 20 |
| 30 |
| 30 |
| 40 |
Table2
| Col1 |
|---|
| 10 |
| 20 |
| 30 |
| 40 |
My expected result is this:
| Col1 | Col2 |
|---|---|
| 10 | Matched |
| 20 | Matched |
| 20 | Not Matched |
| 20 | Not Matched |
| 30 | Matched |
| 30 | Not Matched |
| 40 | Matched |
Query I'm trying to use:
SELECT
T1.Col1,
CASE
WHEN T2.Col1 IS NOT NULL THEN 'Matched'
ELSE 'NotMatched'
END AS Col2
FROM
Table1 T1
LEFT JOIN
Table2 T2 ON T1.Col1 = T2.Col1;
We can stick with your join approach, but use
ROW_NUMBERto keep track of occurrences: