SQL Server - Select first row that meets criteria

前端 未结 4 765
你的背包
你的背包 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:38

    SELECT  MAX(b.ID) AS ID
           ,MAX(Value) AS Value
           ,MAX(OtherCol1) AS OtherCol1
           ,MAX(OtherCol2) AS OtherCol2
           ,MAX(OtherCol3) AS OtherCol3
    FROM TblA AS a 
    INNER JOIN TblB AS b ON a.TblBID = b.ID
    GROUP BY TblBID
    

    Table A

    Table A

    Table B

    Table B

    Table A Data

    Table A Data

    Table B Data

    Table B Data

    Query Result

    Query Result

提交回复
热议问题