求SQL句子 在线等

kalakala 2004-06-26 07:36:44
有这样一张表
id time flag
01 20040626 a
01 20040402 a
01 20031103 b
02 19800123 a
02 20040621 b
03 20040512 a

现在要选出的结果是
01 20040626 a
02 20040621 b
03 20040512 a
就是每个ID对应的最后(最大)的那个时间的flag。
...全文
74 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
kalakala 2004-06-26
  • 打赏
  • 举报
回复
谢谢
ocean9b 2004-06-26
  • 打赏
  • 举报
回复
漏了个and
select id,max(time) 'time',max(select flag from 表 where A.id=id and max(A.time)=time) 'flag' from 表 A group by id
zheninchangjiang 2004-06-26
  • 打赏
  • 举报
回复
select * from yourtable a where [time]=(select max([time]) from yourtable where [id]=a.[id])
zqp1771 2004-06-26
  • 打赏
  • 举报
回复
select * from tablename where id+time in(select id+max(time) from tablename group by id)
trampwind 2004-06-26
  • 打赏
  • 举报
回复
不好意思写错了,select * from tablename where id||time in(select id||max(time) from tablename group by id);
pisces007 2004-06-26
  • 打赏
  • 举报
回复
select b.id,b.time,a.flag from yourtable a join
(select id,max(time) as time from yourtable group by id)b
on a.id=b.id and a.time=b.time
trampwind 2004-06-26
  • 打赏
  • 举报
回复
select * from tablename where id in(select max(id) from tablename group by id);
ocean9b 2004-06-26
  • 打赏
  • 举报
回复
select id,max(time) 'time',max(select flag from 表 where A.id=id,max(A.time)=time) 'flag' from 表 A group by id

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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