表名:tableName 字段 ID,CODE,LogTime
数据如:
ID,CODE,LogTime
1,A1234,2011-09-02 13:12:10
2,A1234,2011-09-03 13:12:10
3,A1237,2011-09-04 13:12:10
4,A1237,2011-09-04 13:12:10
5,A1237,2011-09-05 13:12:10
想要删除的数据
ID,CODE,LogTime
2,A1234,2011-09-03 13:12:10
5,A1237,2011-09-05 13:12:10
SQL:
先找到想要的数据
select Code,max(logtime) from tableName group by Code
哈哈。。。删除不了。
从网上搜了一个如下:
SQL Server
select identity(int,1,1) as autoID, * into #Tmp from tableName;
select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,Address;
drop table tableName;
select * into tableName from #Tmp where autoID in(select autoID from #Tmp2);
drop table #Tmp;
drop table #Tmp2;
改成自己的如下:
select identity(int,1,1) as autoID, * into #Tmp from #table1 Order by logtime; //按时间大排序
select maX(autoID) as autoID into #Tmp2 from #Tmp group by Code //同一CODE的取最大的AUTOID
drop table tableName;
select * into tableName from #Tmp where autoID in(select autoID from #Tmp2);
drop table #Tmp;
drop table #Tmp2;
OVER!