首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

帮忙写个sql语句,该怎么处理

2012-01-23 
帮忙写个sql语句数据库表s的字段如下idvarchar(10)tdatemvarchar(250)...这个表中id和t才唯一确定一条记录

帮忙写个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)

热点排行
Bad Request.