日期:2014-05-18  浏览次数:20472 次

求sql查询不重复记录,并按时间进行排序
数据库字段如下:

name,tel,time

张 133 2010-2-2
李 133 2010-2-3
黄 134 2010-2-4
张 135 2010-2-5
王 135 2010-2-6

现想显示结果如下,要求将tel字段里的重复内容去除


name,tel,time

王 135 2010-2-6
黄 134 2010-2-4
张 133 2010-2-2

------解决方案--------------------
SQL code

declare @t table (name varchar(2),tel int,time varchar(10))
insert into @t
select '张',133,'2010-2-2' union all
select '李',133,'2010-2-3' union all
select '黄',134,'2010-2-4' union all
select '张',135,'2010-2-5' union all
select '王',135,'2010-2-6'


select * from @t t where
time=(select max(time) from @t where tel=t.tel )
order by time desc
/*
name tel         time
---- ----------- ----------
王    135         2010-2-6
黄    134         2010-2-4
李    133         2010-2-3
*/