我有2个包含ID的表。其中一个表中将有重复的ID,我只想为表B中的每个匹配ID返回一行。例如:
表A
+-----------+-----------+ | objectIdA | objectIdB | +-----------+-----------+ | 1 | A | | 1 | B | | 1 | D | | 5 | F | +-----------+-----------+
表B
+-----------+ | objectIdA | +-----------+ | 1 | | 5 | +-----------+
将返回:
+-----------+-----------+ | objectIdA | objectIdB | +-----------+-----------+ | 1 | D | | 5 | F | +-----------+-----------+
我只需要表A中与表B相匹配的一项即可。返回表A的哪一行都没有关系。
我正在使用SQL Server。谢谢。
;WITH CTE AS ( SELECT B.objectIdA ,A.objectIdB ,ROW_NUMBER() OVER (PARTITION BY B.objectIdA ORDER BY A.objectIdB DESC) rn FROM TableA A INNER JOIN TableB B ON A.objectIdA = B.objectIdA ) SELECT C.objectIdA ,C.objectIdB FROM CTE WHERE rn = 1