SQL过滤重复保留一条

newnazi 2016-04-08 10:35:22
已知表:

ID , BarCode , Time
1 , 111 , 2016-04-10 10:00:00
2 , 222 , 2016-04-09 09:00:00
3 , 111 , 2016-04-08 08:00:00
4 , 111 , 2016-0410 10:00:00


要求过滤重复 BarCode 并且保留最新一条
如下:
222 , 2016-04-09 09:00:00
111 , 2016-0410 10:00:00
...全文
1244 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_25073223 2018-02-27
  • 打赏
  • 举报
回复
参阅以下文章: http://www.maomao365.com/?p=4942 将删除修改为 查询就可以了
select *  from 
(select row_number() over (Partition By keyId order by keyId) as keyId2,* from A ) as [A2]
where [A2].keyId2  >1 
听雨停了 2018-01-25
  • 打赏
  • 举报
回复
引用 10 楼 yenange 的回复:
[quote=引用 9 楼 z10843087 的回复:] 忘记结帖了而已吧,,,
不是。 你看楼主在 7 楼还在求建议[/quote] LZ智商余额不足,有待充值
吉普赛的歌 2018-01-25
  • 打赏
  • 举报
回复
引用 9 楼 z10843087 的回复:
忘记结帖了而已吧,,,
不是。 你看楼主在 7 楼还在求建议
OwenZeng_DBA 2018-01-25
  • 打赏
  • 举报
回复
忘记结帖了而已吧,,,
吉普赛的歌 2018-01-25
  • 打赏
  • 举报
回复
DECLARE @t TABLE (
	id INT,
	barcode INT,
	[time] DATETIME
)
INSERT INTO @t(id,barcode,[time])
	      SELECT 1,111,'2016-04-10 10:00:00'
UNION ALL SELECT 2,222,'2016-04-09 09:00:00'
UNION ALL SELECT 3,111,'2016-04-08 08:00:00'
UNION ALL SELECT 4,111,'2016-04-10 10:00:00'

;WITH t AS (
	SELECT ROW_NUMBER() OVER (PARTITION BY barcode ORDER BY [time] DESC) AS rid,* 
	FROM @t
)
DELETE FROM t WHERE rid!=1

SELECT * FROM @t
/*
id	barcode	time
1	111	    2016-04-10 10:00:00.000
2	222	    2016-04-09 09:00:00.000
*/
不过, 楼主是来搞笑的吧? 两年前的贴了, 楼上那么大神的都不能用?
newnazi 2018-01-25
  • 打赏
  • 举报
回复
大神们还有什么好的方法建议????????????
中国风 2016-04-09
  • 打赏
  • 举报
回复
结果集只是查询两列 用#3方法就行了
中国风 2016-04-09
  • 打赏
  • 举报
回复
处理表重复记录(查询和删除) http://bbs.csdn.net/topics/240034273
Ekun_sky 2016-04-08
  • 打赏
  • 举报
回复
with a(ID,BarCode,Time) as(
select 1,111,'2016-04-10 10:00:00' union all
select 2,222,'2016-04-09 09:00:00' union all
select 3,111,'2016-04-08 08:00:00' union all
select 4,111,'2016-0410 10:00:00')


select BarCode,MAX(TIME) AS ATIME from a GROUP BY BarCode

/*
ID    BarCode   Time
-----------------------
111	2016-04-10 10:00:00
222	2016-04-09 09:00:00
*/
spiritofdragon 2016-04-08
  • 打赏
  • 举报
回复

select * from 
(
select * ,rn=ROW_NUMBER()over(partition by BarCode order by [Time])
from t
) tt where tt.rn=1
)
唐诗三百首 2016-04-08
  • 打赏
  • 举报
回复

select a.*
  from [表名] a
  where not exists(select 1 from [表名] b 
                               where b.BarCode=a.BarCode and b.Time>a.Time)
道素 2016-04-08
  • 打赏
  • 举报
回复

            DECLARE @TestTb  Table(ID int,BarCode varchar(100),[Time] datetime)

            insert into @TestTb(ID,BarCode,[Time] )
            select  1  ,'111'   ,   convert(DATETIME,'2016-04-10 10:00:00') union all
            select  2  ,'222'  ,   convert(DATETIME,'2016-04-09 09:00:00') union all
            select  3  ,'111'  ,  convert(DATETIME,'2016-04-08 08:00:00') union all
            select  4  ,'111' ,    convert(DATETIME,'2016-04-10 10:00:00')

         --   select *,row_number()over(PARTITION BY BarCode ORDER BY [time] DESC) as rn FROM @TestTb as t
DELETE tt from( select *,row_number()over(PARTITION BY BarCode ORDER BY [time]) as rn FROM @TestTb as t) as tt where tt.rn!=1
select * from @TestTb
/*
1	111	2016-04-10 10:00:00.000
2	222	2016-04-09 09:00:00.000

*/

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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