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

帮忙写个sql语句
数据库表s的字段如下    
id       varchar(10)
t         date
m         varchar(250)
...

这个表中id和t才唯一确定一条记录
该表中有一系列相同id组成的纪录如下

1       2007/05/12       大幅
1       2008/05/13         死死地
2       2006/11/12         大幅度
2       2007/01/12         得到


现在我想要这个表中相同id所对应的t最大的那些记录
请问这样的sql语句怎么写?

------解决方案--------------------
Select * from S as a
Where not exists(
Select * from s Where id=a.id and t> a.t)

------解决方案--------------------
select * from s as a
where t=(select max(t) from s where id=a.id )
------解决方案--------------------
create table s(id varchar(10), t datetime, m varchar(250))
insert s select 1, '2007/05/12 ', '大幅 '
union all select 1, '2008/05/13 ', '死死地 '
union all select 2, '2006/11/12 ', '大幅度 '
union all select 2, '2007/01/12 ', '得到 '

select * from s as tmp
where not exists(select 1 from s where id=tmp.id and t> tmp.t)

--result
id t m
---------- ------------------------------------------------------ -------------------------
1 2008-05-13 00:00:00.000 死死地
2 2007-01-12 00:00:00.000 得到

(2 row(s) affected)