表kk
字段一 字段二 字段三 字段四
sk yes aaaa 2005-02-02
sk yes aaaa 2005-02-03
sc noo bbbb 2005-02-09
sc noo bbbb 2005-02-05我要得到结果
sk yes aaaa 2005-02-02
sc noo bbbb 2005-02-05也就是前面三个字段相同的作为重复数据,
只保留一条时间最早的数据,SQL怎么写?
谢谢!
字段一 字段二 字段三 字段四
sk yes aaaa 2005-02-02
sk yes aaaa 2005-02-03
sc noo bbbb 2005-02-09
sc noo bbbb 2005-02-05我要得到结果
sk yes aaaa 2005-02-02
sc noo bbbb 2005-02-05也就是前面三个字段相同的作为重复数据,
只保留一条时间最早的数据,SQL怎么写?
谢谢!
解决方案 »
- 这种条件判断怎么写成一个函数。
- 求一条SQL 语句..效率最高
- 项目合并问题,请高手解答
- 请问怎么恢复sp_password这个存储过程,谢谢,
- 我现在想把itemno重复的去掉,但我要adjustno里面最大的一个,这个代码怎么写啊???
- 这个问题我也不知道怎么问,关于进入sql时的一些问题,^_^
- 如何看临时表中的信息
- 我在“控制中心”中通过双击tableList看到的是AlterTable的脚本,有什么办法可以看到CreatTable的脚本!
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~用Delphi的程序调用DTS对于相同的条件,却每次返回不同的结果~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- oracle热备份问题
- SQL中重复数据的删除问题,请教
- 筛选重复数据新问题,求高手帮我解决.急!!!!!!!!
insert into @t select 'sk' , 'yes' , 'aaaa' , '2005-02-02'
union all select 'sk' , 'yes' , 'aaaa' , '2005-02-03'
union all select 'sc' , 'noo' , 'bbbb' , '2005-02-09'
union all select 'sc' , 'noo' , 'bbbb' , '2005-02-05'delete @t from @t a where exists(select 1 from @t where 字段一=a.字段一 and 字段二=a.字段二 and 字段三=a.字段三 and 字段四<a.字段四)select * from @t
from 表kk
group by 字段一 , 字段二, 字段三
where not exists (select 1 from kk k2 where k2.字段1=k1.字段1 and k2.字段2=k1.字段2 and k2.字段3 = k1.字段3 and k1.字段4>k2.字段4)
truncate tb
insert into tb select * from #tb
insert into t3 select 'sk','yes','aaa','2005-02-02'
union all select 'sk','yes','aaa','2005-02-03'
union all select 'sc','noo','bbb','2005-02-09'
union all select 'sc','noo','bbb','2005-02-05'
--select * from t3
select f1,f2,f3,min(f4) from t3
group by f1,f2,f3drop table t3
from kk a
where exists(select * from kk where 字段1=a.字段1 and 字段2=a.字段2
and 字段3=a.字段3 and 字段4>a.字段4)
或
delete a
from kk a,kk b
where b.字段1=a.字段1 and b.字段2=a.字段2
and b.字段3=a.字段3 and b.字段4>a.字段4)
create table t3(f1 nvarchar(10),f2 nvarchar(10),f3 nvarchar(10),f4 datetime)
insert into t3 select 'sk','yes','aaa','2005-02-02'
union all select 'sk','yes','aaa','2005-02-03'
union all select 'sc','noo','bbb','2005-02-09'
union all select 'sc','noo','bbb','2005-02-05'
--执行查询
delete t3
from t3 a
where exists(select * from t3 where f1=a.f1 and f2=a.f2 and f3=a.f3 and f4<a.f4)--select * from t3
--删除表
DROP table t3
sk yes aaaa 2005-02-03
sc noo bbbb 2005-02-09
sc noo bbbb 2005-02-05
sc noo bbbb 2005-02-06
sc noo bbbb 2005-02-07
sc noo bbbb 2005-02-08
要得到
sk yes aaaa 2005-02-02
sc noo bbbb 2005-02-05
如果是这样的数据话,你使用的f4<a.f4条件是不行的
create table t3(f1 nvarchar(10),f2 nvarchar(10),f3 nvarchar(10),f4 datetime)
insert into t3 select 'sk','yes','aaaa','2005-02-02'
union all select 'sk','yes','aaaa','2005-02-03'
union all select 'sc','noo','bbbb','2005-02-09'
union all select 'sc','noo','bbbb','2005-02-05'
union all select 'sc','noo','bbbb','2005-02-06'
union all select 'sc','noo','bbbb','2005-02-07'
union all select 'sc','noo','bbbb','2005-02-08'
--执行查询
delete t3
from t3 a
where exists(select * from t3 where f1=a.f1 and f2=a.f2 and f3=a.f3 and f4<a.f4)--select * from t3
--删除表
DROP table t3