当前位置: 代码迷 >> Sql Server >> 去掉其他相似数据,保留最大时间数据解决思路
  详细解决方案

去掉其他相似数据,保留最大时间数据解决思路

热度:27   发布时间:2016-04-27 15:16:44.0
去掉其他相似数据,保留最大时间数据
create table tb
(
id int
,date datetime
,dvalue int
)


insert into tb 
select 1,'2011-12-30 19:29:00',1
union all select 1,'2011-12-30 19:36:00',2
union all select 2,'2011-12-30 20:29:00',3
union all select 2,'2011-12-30 20:31:00',4
union all select 3,'2011-12-30 21:00:00',5
union all select 4,'2011-12-30 22:00:00',6
union all select 5,'2011-12-30 23:00:00',7

-------显示结果-------
1,'2011-12-30 19:36:00',2
2,'2011-12-30 20:31:00',4
3,'2011-12-30 21:00:00',5
4,'2011-12-30 22:00:00',6
5,'2011-12-30 23:00:00',7

------解决方案--------------------
让楼下来吧。
------解决方案--------------------
SQL code
select * from tb as t where not exists(select 1 from tb where ID=a.ID and (date>a.date or(Date=a.Date and dvalue>a.dvalue)))
  相关解决方案