请教 select top x with ties.....的用法

skyboy0720 2004-05-04 03:56:10
今天看了本书,上面说了一下with ties这种用法,我自己
测试
create table cc
(id int identity(1,1),
name varchar(10),
sort int
)

insert into cc select 'a',21
union all select 'b',21
union all select 'c',22
union all select 'd',22
union all select 'e',22
union all select 'f',23
union all select 'g',26


select top 3 with ties id,name,sort from cc order by sort

---结果是

id name sort
1 a 21
2 b 21
3 d 22
4 e 22
5 f 22

不明白结果为什么是这样,能解释它的用法吗!!
...全文
108 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
skyboy0720 2004-05-04
  • 打赏
  • 举报
回复
明白了,谢谢!
txlicenhe 2004-05-04
  • 打赏
  • 举报
回复
相当于:
select * from cc where sort in (select top 3 sort from cc)
txlicenhe 2004-05-04
  • 打赏
  • 举报
回复
指定从基本结果集中返回附加的行,这些行包含与出现在 TOP n (PERCENT) 行最后的 ORDER BY 列中的值相同的值。如果指定了 ORDER BY 子句,则只能指定 TOP ...WITH TIES。

zjcxc 元老 2004-05-04
  • 打赏
  • 举报
回复
按sort进行order by ,top 3 的最后一条记录的sort值是22
所有后面的记录中,sort值为22的都列出来了.

34,838

社区成员

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

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