求一select语句:取字段最大值

三色 2011-08-09 11:15:02
表tbl结构如下
model sort time
modelA 创建 2011-1-1 1:0:0
modelA 删除 ...
modelA 修改 ...
modelA 修改 ...
modelA 修改 ...
modelB 创建 ...
modelB 删除 ...
modelB 修改 ...
modelB 修改 ...
modelC 修改 ...
...

想要select的结果条件
1,sort='删除'
2,time取同一model下最大值

请问select语句怎么写
...全文
308 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
--小F-- 2011-08-09
  • 打赏
  • 举报
回复
select * from tbl t where sort='删除' and time=(select max(time) from tbl where model=t.model) 
chuanzhang5687 2011-08-09
  • 打赏
  • 举报
回复
select * from tbl a
where sort='删除' and time=(select max(time) from tbl where model=a.model
快溜 2011-08-09
  • 打赏
  • 举报
回复
select * from tbl a
where sort='删除' and time=(select max(time) from tbl where model=a.model
三色 2011-08-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 nbdba 的回复:]
SQL code

select * from tbl a
where sort='删除'
and not exists (
select 1
from tbl
where model=a.model
and time>a.time
)
[/Quote]

测试通过,谢谢
AcHerat 元老 2011-08-09
  • 打赏
  • 举报
回复

select model,max(time) time
from tb
where sort = '删除'
group by model
NBDBA 2011-08-09
  • 打赏
  • 举报
回复
从搂主得表述理解写的,可能没有数据返回
因为搂主没说明
2,time取同一model下最大值
这个条件是否受
1,sort='删除'
限制
NBDBA 2011-08-09
  • 打赏
  • 举报
回复
select * from tbl a
where sort='删除'
and not exists (
select 1
from tbl
where model=a.model
and time>a.time
)

34,590

社区成员

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

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