求助一句SQL (删除重复数据只保留时间最晚的一条)

HarleyTung 2008-10-20 12:41:58
数据类型:
Code char(6)
CreateTime datetime
Price float
数据如下:
Code CreateTime Price
031002 2008-10-17 15:00:15 4.558
031002 2008-10-17 15:00:16 4.554
031002 2008-10-17 15:00:17 4.554
031002 2008-10-17 15:00:18 4.561
031003 2008-10-17 15:00:19 4.562
031003 2008-10-17 15:00:20 4.562
031003 2008-10-17 15:00:21 4.562
031004 2008-10-17 15:00:22 4.565
031004 2008-10-17 15:00:23 4.565
031004 2008-10-17 15:00:24 4.567

我想要的效果是删除Code和Price相同的数据,只保留时间最晚的一条

结果为:
Code CreateTime Price
031002 2008-10-17 15:00:15 4.558
031002 2008-10-17 15:00:17 4.554
031002 2008-10-17 15:00:18 4.561
031003 2008-10-17 15:00:21 4.562
031004 2008-10-17 15:00:23 4.565
031004 2008-10-17 15:00:24 4.567

请问这句SQL怎么写?谢谢!
...全文
2023 32 打赏 收藏 转发到动态 举报
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
CN_SQL 2008-10-20
  • 打赏
  • 举报
回复

declare @t table
(
Code char(6),
CreateTime datetime,
Price float
)

insert @t select '031002','2008-10-17 15:00:15',4.558
union all select '031002','2008-10-17 15:00:16',4.554
union all select '031002','2008-10-17 15:00:17',4.554
union all select '031002','2008-10-17 15:00:18',4.561
union all select '031003','2008-10-17 15:00:19',4.562
union all select '031003','2008-10-17 15:00:20',4.562
union all select '031003','2008-10-17 15:00:21',4.562
union all select '031004','2008-10-17 15:00:22',4.565
union all select '031004','2008-10-17 15:00:23',4.565
union all select '031004','2008-10-17 15:00:24',4.567

delete a
from @t a
where exists(
select *
from @t
where Code = a.Code
and Price = a.Price
and CreateTime > a.CreateTime)

select * from @t
CN_SQL 2008-10-20
  • 打赏
  • 举报
回复
[Quote=引用 30 楼 dyjqk 的回复:]
to:CN_SQL 不好意思,我执行了
delete a
from (
select
row_num = row_number() over(partition by Code,Price order by CreateTime desc)
from @t
)a
少了where row_num > 1

对不起,是我漏了,我结贴,特别感谢CN_SQL帮助,也感谢其它回帖的朋友!
[/Quote]
解决了就好。
cxmcxm 2008-10-20
  • 打赏
  • 举报
回复
--设表名为t
delete a from t as a where exists(select * from t where code=a.code and price=a.price and createtime>a.createtime)
HarleyTung 2008-10-20
  • 打赏
  • 举报
回复
to:CN_SQL 不好意思,我执行了
delete a
from (
select
row_num = row_number() over(partition by Code,Price order by CreateTime desc)
from @t
)a
少了where row_num > 1

对不起,是我漏了,我结贴,特别感谢CN_SQL帮助,也感谢其它回帖的朋友!
CN_SQL 2008-10-20
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 dyjqk 的回复:]
delete a
from (
select
row_num = row_number() over(partition by Code,Price order by CreateTime desc)
from @t
)a
where row_num > 1

用这句就全部删掉了,不会保留时间最晚的那条
[/Quote]
你有没运行我给你的代码,
我不知道你的问题到底在哪?
HarleyTung 2008-10-20
  • 打赏
  • 举报
回复
delete a
from (
select
row_num = row_number() over(partition by Code,Price order by CreateTime desc)
from @t
)a
where row_num > 1

用这句就全部删掉了,不会保留时间最晚的那条
CN_SQL 2008-10-20
  • 打赏
  • 举报
回复
你把数据换成下面这个,也没问题啊:


insert @t select '031002','2008-10-17 15:00:15',4.558
union all select '031002','2008-10-17 15:00:15',4.558
union all select '031002','2008-10-17 15:00:15',4.558
union all select '031002','2008-10-17 15:00:15',4.558
union all select '031003','2008-10-17 15:00:19',4.562
union all select '031003','2008-10-17 15:00:19',4.562
union all select '031003','2008-10-17 15:00:19',4.562
union all select '031004','2008-10-17 15:00:22',4.565
union all select '031004','2008-10-17 15:00:22',4.565
union all select '031004','2008-10-17 15:00:22',4.565
CN_SQL 2008-10-20
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 dyjqk 的回复:]
to:CN_SQL 就是要这个效果,就是说 代码 价格 一样的情况下,小于等于最后一条时间记录的都要删掉,就保留时间最晚的一条。谢谢!
[/Quote]
那我给你的2005的方法就没问题啊:


declare @t table
(
Code char(6),
CreateTime datetime,
Price float
)

insert @t select '031002','2008-10-17 15:00:15',4.558
union all select '031002','2008-10-17 15:00:16',4.554
union all select '031002','2008-10-17 15:00:17',4.554
union all select '031002','2008-10-17 15:00:18',4.561
union all select '031003','2008-10-17 15:00:19',4.562
union all select '031003','2008-10-17 15:00:20',4.562
union all select '031003','2008-10-17 15:00:21',4.562
union all select '031004','2008-10-17 15:00:22',4.565
union all select '031004','2008-10-17 15:00:22',4.565
union all select '031004','2008-10-17 15:00:22',4.565



delete a
from (
select
row_num = row_number() over(partition by Code,Price order by CreateTime desc)
from @t
)a
where row_num > 1

select * from @t


/**
031002 2008-10-17 15:00:15.000 4.558
031002 2008-10-17 15:00:17.000 4.554
031002 2008-10-17 15:00:18.000 4.561
031003 2008-10-17 15:00:21.000 4.562
031004 2008-10-17 15:00:22.000 4.565
**/
HarleyTung 2008-10-20
  • 打赏
  • 举报
回复
我是SQL Server 2005 库,希望能一句话解决,如果两句话解决不影响性能的话也可以,顺便说下,这个表有10几万条数据,我已经对Code和Price做了聚集索引。
HarleyTung 2008-10-20
  • 打赏
  • 举报
回复
to:CN_SQL 就是要这个效果,就是说 代码 价格 一样的情况下,小于等于最后一条时间记录的都要删掉,就保留时间最晚的一条。谢谢!
  • 打赏
  • 举报
回复
经典的代码
CN_SQL 2008-10-20
  • 打赏
  • 举报
回复

declare @t table
(
Code char(6),
CreateTime datetime,
Price float
)

insert @t select '031002','2008-10-17 15:00:15',4.558
union all select '031002','2008-10-17 15:00:15',4.558
union all select '031002','2008-10-17 15:00:15',4.558
union all select '031002','2008-10-17 15:00:15',4.558
union all select '031003','2008-10-17 15:00:19',4.562
union all select '031003','2008-10-17 15:00:19',4.562
union all select '031003','2008-10-17 15:00:19',4.562
union all select '031004','2008-10-17 15:00:22',4.565
union all select '031004','2008-10-17 15:00:22',4.565
union all select '031004','2008-10-17 15:00:22',4.565


/**
希望的结果
031002 2008-10-17 15:00:15.000 4.558
031003 2008-10-17 15:00:19.000 4.562
031004 2008-10-17 15:00:22.000 4.565
**/

是不是这个意思?
CN_SQL 2008-10-20
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 dyjqk 的回复:]
大哥,有办法吗?
[/Quote]
非要一句SQL?
HarleyTung 2008-10-20
  • 打赏
  • 举报
回复
大哥,有办法吗?
HarleyTung 2008-10-20
  • 打赏
  • 举报
回复
我的库是SQL SERVER 2005
CN_SQL 2008-10-20
  • 打赏
  • 举报
回复
如果你的意思是删除重复记录,且你的表就这个设计,2000下是无法一句SQL完成。
HarleyTung 2008-10-20
  • 打赏
  • 举报
回复
大家帮忙看下,谢谢了!
fzcheng 2008-10-20
  • 打赏
  • 举报
回复

delete a
from tb a
where exists(select * from @t
where Code = a.Code and Price = a.Price and CreateTime > a.CreateTime)

HarleyTung 2008-10-20
  • 打赏
  • 举报
回复
解决立刻结贴,谢谢!
时光瞄 2008-10-20
  • 打赏
  • 举报
回复
delete a from @t where createTime<any(select createTime from @t where code=a.code and price=a.price)
加载更多回复(12)

34,591

社区成员

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

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