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;
4条答案
按热度按时间n53p2ov01#
We can stick with your join approach, but use
ROW_NUMBER
to keep track of occurrences:bbmckpt72#
Run
xuo3flqw3#
Here is the solution
hfwmuf9z4#
Assuming table2 contains distinct values only, you can use
row_number()
to assign a unique number to each row thenleft join
first record per partition only :Demo here