如何 select 来自 SQL 服务器列的前 3 个最大值

How to select top 3 maximum values from SQL Server column

我在 SQL 服务器派生列中有值按降序排序,即

id   Count    OR     id   Count    OR     id   Count
1    4               1    5               1    11
2    4               2    2               2    1
3    4               3    1               3    1
4    4               4    1               4    1
5    4               5    1               5    1

现在我想要 select 前 3 个最大值。我怎样才能 select 每次查询 returns 一致的结果。
例如,如果 Count 的值相同,则 id 应作为前 3 个最大值返回,类似地,如果第三个值与其他值匹配,如果第二个值与其他值匹配,则 id's 应该由查询返回。每次执行查询时结果应该是一致的。

top 函数的 with ties 参数将 return 匹配最高值的所有行:

select top (3) with ties id, count from table1
order by count desc

或者,如果您只想 return 3 个值,但要确保它们始终是相同的 3 个值,那么您将需要使用其他东西作为决胜局。在这种情况下,您的 id 列看起来可能是唯一的。

select top (3) id, count from table1
order by count desc, id