Forum Discussion
phanpa
Jul 11, 2022Copper Contributor
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...
- Jul 12, 2022As 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
benweissman
Jul 12, 2022Copper Contributor
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
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