一條簡單的sql

zhuqiang9 2006-11-14 02:25:13

id time type

a 2006-11-14 13:55:17.320 net
b 2006-11-14 13:55:54.980 c++
a 2006-11-14 13:57:04.963 c
b 2006-11-14 14:00:29.843 java
c 2006-11-14 14:00:51.763 sql
我想select a,b,c這3种編號最新的一條數據
...全文
159 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
allright_flash 2006-11-14
  • 打赏
  • 举报
回复
select id,max(time) from tablename group by id
中国风 2006-11-14
  • 打赏
  • 举报
回复
select * from @ta a where time=any(select max(time) from @ta group by id)
id time type
---- ------------------------------------------------------ ----------
a 2006-11-14 13:57:04.963 c
b 2006-11-14 14:00:29.843 java
c 2006-11-14 14:00:51.763 sql

(所影响的行数为 3 行)
中国风 2006-11-14
  • 打赏
  • 举报
回复
棒场!
declare @ta table ([id] varchar(2), [time] datetime, type varchar(10))
insert @ta
select 'a', '2006-11-14 13:55:17.320' , 'net' union all
select 'b' , '2006-11-14 13:55:54.980' , 'c++' union all
select 'a' , '2006-11-14 13:57:04.963' , 'c' union all
select 'b' , '2006-11-14 14:00:29.843' , 'java' union all
select 'c' , '2006-11-14 14:00:51.763', 'sql'
select * from @ta a where time=(select max(time) from @ta where id=a.id) order by time asc

(所影响的行数为 5 行)

id time type
---- ------------------------------------------------------ ----------
a 2006-11-14 13:57:04.963 c
b 2006-11-14 14:00:29.843 java
c 2006-11-14 14:00:51.763 sql

(所影响的行数为 3 行)

jamesming 2006-11-14
  • 打赏
  • 举报
回复
要是LZ的id中有E,F,G,**,二楼的方法也行吗?
fish_yht 2006-11-14
  • 打赏
  • 举报
回复
理解错误
不好意思
crazyflower 2006-11-14
  • 打赏
  • 举报
回复
select id,max(time) from tb where id in ('a','b','c') group by id
fish_yht 2006-11-14
  • 打赏
  • 举报
回复
select top 1 * from 表
order by time desc
子陌红尘 2006-11-14
  • 打赏
  • 举报
回复
select t.* from 表 t where not exists(select 1 from 表 where id=t.id and time>t.time)
冷箫轻笛 2006-11-14
  • 打赏
  • 举报
回复
select * from tablename t1
where not exists (select 1 from tablename where id = t1.id and time > t1.time)

34,590

社区成员

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

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