读书人

求sql查询不重复记录并按时间进行排

发布时间: 2012-03-17 19:06:28 作者: rapoo

求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 @tselect '张',133,'2010-2-2' union allselect '李',133,'2010-2-3' union allselect '黄',134,'2010-2-4' union allselect '张',135,'2010-2-5' union allselect '王',135,'2010-2-6'select * from @t t wheretime=(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*/ 

读书人网 >SQL Server

热点推荐