SQL Server - Select first row that meets criteria

前端 未结 4 722
你的背包
你的背包 2021-01-29 06:18

I have 2 tables that contain IDs. There will be duplicate IDs in one of the tables and I only want to return one row for each matching ID in table B. For example:

Table

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-29 06:47

    ;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
    

提交回复
热议问题