Forum Discussion

phanpa's avatar
phanpa
Copper Contributor
Jul 11, 2022

Duplicate Records

Hi, How do a write a view in SQL Server that pulls the most updated record so there's no duplicate id?   Example: id     status           date 123  pending       2022-02-25 123  completed   202...
  • benweissman's avatar
    Jul 12, 2022
    As Olaf said, a little more Info would be helpful.

    Anyway, assuming your date is actually a datetime, what you could do:

    SELECT a.* FROM [table] a
    INNER JOIN
    (SELECT id,max(date) [date] from [table] group by id) b
    ON a.id = b.id and a.[date] = b.[date]

    There are multiple other ways depending on the actual data structure.

    Ben

Resources