문제
데이터베이스 백업에서 고유 한 ID가없는 레코드가 있습니다.
일부 레코드에는 고유 한 ID가 있습니다. 복제 된 IDS가있는 일부 레코드에는 다른 DateCreated 값이 포함됩니다. 복제 된 ID가있는 일부 레코드에는 동일한 DateCreated 값이 포함됩니다.
MSSQL 2005 쿼리를 얻으려고 노력하고 있습니다. 가장 최근의 DateCreated 값을 가진 고유 ID 값 만 남길 것입니다.
에서
ID| DateCreated
1 | 1/1/09
2 | 1/2/09
2 | 2/2/09
3 | 1/3/09
3 | 1/3/09
에게
ID| DateCreated
1 | 1/1/09
2 | 2/2/09
3 | 1/3/09
돕다
해결책
DELETE FROM myTable AS t1
WHERE EXISTS (
SELECT 1 FROM myTable AS t2
WHERE t1.ID=t2.ID AND t1.DateCreated<t2.DateCreated)
즉, 동일한 ID와 이후의 생성 날짜가있는 다른 행이있는 행을 삭제하십시오.
다른 팁
create table #t ( id int, date datetime )
insert #t
values(1, getdate())
insert #t
values(1, getdate()+1)
insert #t
values(1, getdate()-1)
insert #t
values(2, getdate())
insert #t
values(2, getdate()+1)
delete t
from #t t
left join (select id, min(date) as date from #t group by id) as t1
on t.id = t1.id and t1.date = t.date
where t1.date is null
제휴하지 않습니다 StackOverflow