从一个表按照类型取top1如何实现

coldljy 2006-02-20 04:25:43
表结构相关字段:
Id 标识 PK
TypeId 类型标识 FK
Sequence 顺序

要求按照顺序进行排序(可递增、可递减),取到每种类型的第一条记录
btw.性能第一
...全文
93 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhaoanle 2006-02-20
  • 打赏
  • 举报
回复
select * from tbl a where id=(select top 1 id from tbl where typeid=a.typeid order by sequence)
lsxaa 2006-02-20
  • 打赏
  • 举报
回复

分组取最大
select a.*
from 表 a,(select typeid,max(sequence) as sequence from 表 group by typeid) b
where a.typeid=b.typeid and a.sequence=b.sequence

分组取最小
select a.*
from 表 a,(select typeid,min(sequence) as sequence from 表 group by typeid) b
where a.typeid=b.typeid and a.sequence=b.sequence

22,300

社区成员

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

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