求一个sql语句~~~大侠拜托了。。。

zzw165027967 2012-04-11 10:31:07
比如有个表table
id col datetime
1 aa 2012-01-01
2 aa 2012-01-03
3 bb 2012-03-01
4 cc 2011-10-12
5 bb 2013-11-11

我要得到
2 aa 2012-01-03
5 bb 2013-11-11
4 cc 2011-10-12


就是col字段相同的,只取datetime最后的那一条记录~~拜托了~~
...全文
51 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
迪迦凹凸曼 2012-04-11
  • 打赏
  • 举报
回复
select id,col,max(datetime)
from tb
group by id,col
AcHerat 2012-04-11
  • 打赏
  • 举报
回复

select *
from tb t
where id = (select top 1 id from tb where col=t.col order by date desc)

select *
from tb t
where date = (select max(date) from tb where col=t.col)

select *
from(
select *,rid=row_number() over (partition by col order by date desc)
from tb
)t
where rid = 1

-- ...
AcHerat 2012-04-11
  • 打赏
  • 举报
回复

select *
from tb t
where not exists (select 1 from tb where col=t.col and date>t.date)

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧