Join to only the “latest” record with t-sql

前端 未结 7 895
你的背包
你的背包 2020-12-08 08:28

I\'ve got two tables. Table \"B\" has a one to many relationship with Table \"A\", which means that there will be many records in table \"B\" for one record in table \"A\".<

相关标签:
7条回答
  • 2020-12-08 08:57

    table B join is optional: it depends if there are other columns you want

    SELECT
        *
    FROM
        tableA A
        JOIN
        tableB B ON A.ID = B.TableAID
        JOIN
        (
        SELECT Max(RowDate) AS MaxRowDate, TableAID
        FROM tableB
        GROUP BY TableAID
        ) foo ON B.TableAID = foo.TableAID AND B.RowDate= foo.MaxRowDate
    
    0 讨论(0)
提交回复
热议问题