去重复的问题,急求。

TLJewel 2007-03-22 03:43:01
我建了一张表,里边包括 ID,时间和 TAG,表里很多TAG和ID是重复的,现在我想去除TAG的重复,但要获取最新时间的ID,语句该怎么写?
...全文
212 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
TLJewel 2007-03-26
  • 打赏
  • 举报
回复
已经搞定了,2楼的朋友帮了很大的忙,谢谢啊。
TLJewel 2007-03-26
  • 打赏
  • 举报
回复
谢谢大家,正在试
XJY123 2007-03-23
  • 打赏
  • 举报
回复
select identity(int,1,1) as autoID, * into #Tmp from tableName
select min(autoID) as autoID into #Tmp2 from #Tmp group by ID,autoID
select * from #Tmp where autoID in(select autoID from #tmp2)
最后一个select即得到了ID,Tag不重复的结果集(但多了一个autoID字段,实际写时可以写在select子句中省去此列)
新鲜鱼排 2007-03-23
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/5073/5073353.xml?temp=.7302973
新鲜鱼排 2007-03-23
  • 打赏
  • 举报
回复
create table #t(tag int, _date datetime)
insert #t select 1, '2006-06-07'
union all select 1,'2007-02-03'
union all select 1,'2007-02-04'
union all select 2,'2007-02-03'

delete from #t
where convert(varchar,tag) + '_' + convert(varchar,_date ,120)
not in (select convert(varchar,tag) + '_' + convert(varchar,max(_date),120) from #t group by tag)

select * from #t

/*
tag _date
----------- ------------------------------------------------------
1 2007-02-04 00:00:00.000
2 2007-02-03 00:00:00.000
*/

drop table #t

34,593

社区成员

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

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