发帖求助:关于查询

super_miker 2013-01-03 01:31:06
有一个表,有3个列:ID、Name、Time
其中ID是Int类型,Name是Varchar类型,Time是DateTime类型

现要查询:对于每个ID,求其Time列最大值所对应的Name,语句该如何写?

先谢过大家
...全文
412 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
menggang9801 2013-01-16
  • 打赏
  • 举报
回复
引用 8 楼 lemongrassstaste 的回复:
select [ID],[Name],[Time] from Table a inner join ( select [ID],MAX([Time]) from Table b group by [ID] ) c where a.[ID]= c.[ID] and a.[Time] = c.[Time]
我写的也是这个
-晴天 2013-01-06
  • 打赏
  • 举报
回复
select * from tb a where not exists(select 1 from tb where id=a.id and time>a.time)
找猫画虎 2013-01-05
  • 打赏
  • 举报
回复
select name from (select *,row=row_number()over(partition by id order by time desc) from tb)t where row=1 写的很简介呀!
浪花三朵朵 2013-01-05
  • 打赏
  • 举报
回复
select t2.Name from Test t2 join (select ID,MAX(Time) time from Test t group by ID) t1 on t2.ID=t1.ID and t2.Time=t1.time
曲军昌 2013-01-04
  • 打赏
  • 举报
回复
select id,name from 表 where time=max(time) group by id
FelixOu 2013-01-03
  • 打赏
  • 举报
回复
select [ID],[Name],[Time] from Table a inner join ( select [ID],MAX([Time]) from Table b group by [ID] ) c where a.[ID]= c.[ID] and a.[Time] = c.[Time]
波西米亚人生 2013-01-03
  • 打赏
  • 举报
回复
select id,name from MYTABLE a where exists (select 1 from MYTABLE where a.id=id group by id having max(time)=a.time)
belone99 2013-01-03
  • 打赏
  • 举报
回复
select id,max(name),max(time) from table group by id
szm341 2013-01-03
  • 打赏
  • 举报
回复

select * from tb a where exists (select 1 from tb where a.id=id
group by id having max(time)=a.time) 
發糞塗牆 2013-01-03
  • 打赏
  • 举报
回复
SELECT  *
FROM    TB a
WHERE   EXISTS ( SELECT 1
                 FROM   ( SELECT    id ,
                                    MAX([time]) [time]
                          FROM      TB
                          GROUP BY  id
                        ) b
                 WHERE  a.id = b.id
                        AND a.[time] = b.[time] )
xiaoqi7456 2013-01-03
  • 打赏
  • 举报
回复
select * from tb a where not exists (select 1 from tb b where a.id=b.id and b.time>a.time)
  • 打赏
  • 举报
回复
select name from (select *,row=row_number()over(partition by id order by time desc) from tb)t where row=1
a320898 2013-01-03
  • 打赏
  • 举报
回复
好共同认识他突然很听话
哥哥大 2013-01-03
  • 打赏
  • 举报
回复
如果ID值重复,并每个ID都有大小值的time,sql如下: use aa select name from cs where time in (select max(time) from cs group by id) 我的语句是最简单的。
哥哥大 2013-01-03
  • 打赏
  • 举报
回复
加入你的数据库名为 aa,表名为cs,SQL如下: use aa select name from cs where time in (select max(time) from cs)
wanggang_sql 2013-01-03
  • 打赏
  • 举报
回复
没看到你要得到的查询结果,请说清楚

34,576

社区成员

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

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