过滤出重复数据

骑驴快跑 2010-09-26 07:27:19
现在有一张表,有年、月、日、时、分,数据值这几个字段,把年月日时分设置为主键的话就说有重复数据,用什么SQL语句能过滤出来重复的值,把其删除
...全文
108 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
hil2000 2010-09-27
  • 打赏
  • 举报
回复
学习了
baiying15 2010-09-27
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sqlcenter 的回复:]
SQL code
;with cte as
(
select rn = row_number()over(partition by 年,月,日,时,分 order by getdate()), * from 有一张表
)
delete from cte where rn > 1
[/Quote]

经典。。。
happy664618843 2010-09-27
  • 打赏
  • 举报
回复

delete a from tb as a where ID not exists(select max(ID) from tb)
tlx20093A 2010-09-26
  • 打赏
  • 举报
回复
学习了
飘零一叶 2010-09-26
  • 打赏
  • 举报
回复

delete a from tb a where exists (select 1 from (select row_number() over(order by getdate()) as id,* from tb) x where a.id>x.id and a.年=x.年 and a.月=x.月 and a.日=x.日 and a.时=x.时 and a.分=x.分)
Rotel-刘志东 2010-09-26
  • 打赏
  • 举报
回复
select * from 表名1 a where exists (select 1 from 表名 where 表名.年=a.年 and 表名.月=a.月 and 表名.日= a.日
and 表名.时 = a.时 and 表名.分 = a.分)
喜-喜 2010-09-26
  • 打赏
  • 举报
回复
use test
go
if object_id('test.dbo.tb') is not null drop table tb
-- 创建数据表
create table tb
(
年 int,
月 int,
日 int,
时 int,
分 int
)
go
--插入测试数据
insert into tb select 2010,3,5,10,35
union all select 2010,3,5,10,35
union all select 2010,3,5,10,35
union all select 2010,7,15,9,24
union all select 2010,7,15,9,24
union all select 2010,9,20,6,43
go
--代码实现

select * from tb

/*测试结果

年 月 日 时 分
---------------------------------------------
2010 3 5 10 35
2010 3 5 10 35
2010 3 5 10 35
2010 7 15 9 24
2010 7 15 9 24
2010 9 20 6 43

(6 行受影响)
*/

;with t as(select idd=row_number()over(partition by 年,月,日,时,分 order by getdate()),* from tb)
delete t from t where idd!=1

select * from tb

/*测试结果

年 月 日 时 分
---------------------------------------------
2010 3 5 10 35
2010 7 15 9 24
2010 9 20 6 43

(3 行受影响)
*/
SQLCenter 2010-09-26
  • 打赏
  • 举报
回复
;with cte as
(
select rn = row_number()over(partition by 年,月,日,时,分 order by getdate()), * from 有一张表
)
delete from cte where rn > 1
dawugui 2010-09-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 stmn1320 的回复:]
引用 1 楼 dawugui 的回复:
假设还有个字段ID,能根据年、月、日、时、分区分大小。

delete tb from tb t where id not in (select min(id) from tb where 年 = t.年 and 月=t.月 and 日 = t.日 and 时 = t.时 and 分=t.分)


select * from 表名1 a wher……
[/Quote]


select * from 表名1 a where exists (select 1 from 表名 where 表名.年=a.年 and 表名.月=a.月 and 表名.日= a.日
and 表名.时 = a.时 and 表名.分 = a.分)
feixianxxx 2010-09-26
  • 打赏
  • 举报
回复
select * 
from 表名1 a
where exists (select 1 from 表名 where 表名.年=a.年 and 表名.月=a.月 and 表名.日= a.日
and 表名.时 = a.时 and 表名.分 = a.分)

这样就过滤后粗来了
骑驴快跑 2010-09-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 dawugui 的回复:]
假设还有个字段ID,能根据年、月、日、时、分区分大小。

delete tb from tb t where id not in (select min(id) from tb where 年 = t.年 and 月=t.月 and 日 = t.日 and 时 = t.时 and 分=t.分)
[/Quote]

select * from 表名1 a where not exists (select 1 from 表名 where 表名.年=a.年 and 表名.月=a.月 and 表名.日= a.日
and 表名.时 = a.时 and 表名.分 = a.分)

我是这么写的,但是没过滤出来有重复数据呢,您写的假设没ID这个字段呢
dawugui 2010-09-26
  • 打赏
  • 举报
回复
假设还有个字段ID,能根据年、月、日、时、分区分大小。

delete tb from tb t where id not in (select min(id) from tb where 年 = t.年 and 月=t.月 and 日 = t.日 and 时 = t.时 and 分=t.分)

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧