[英]Select max entries in a range in SQL server
I have a salary column where I have to select between range of 10000 and 20000 and also the top rows of max salary.我有一个薪水列,我必须在 10000 到 20000 之间的 select 以及最高薪水的前几行。
Column I have:我有的专栏:
25000
17000
17000
15000
14000
9000
Rows I want to select我想要的行 select
17000
17000
17000
I can use top n rows but that will apply on to this column.我可以使用前 n 行,但这将适用于该列。 What I want to do is, say if this column has 2 rows that has max salary, it would select 2, but if another column has 5 rows that has max salary like below, then it should select 5. column I have:
我想要做的是,假设该列有 2 行具有最高薪水,它将 select 2,但如果另一列有 5 行具有如下的最高薪水,那么它应该 select 5。我有:
24000
17000
17000
17000
17000
17000
15000
14000
9000
rows I want to select:我想要 select 的行:
17000
17000
17000
17000
17000
my sql statement:我的 sql 声明:
select max(salary)
from table
where salary between 10000 and 20000
order by salary desc;
This statements only returns 1 row.此语句仅返回 1 行。
You may use TOP 1 WITH TIES
here:您可以在这里使用
TOP 1 WITH TIES
:
SELECT TOP 1 WITH TIES salary
FROM yourTable
WHERE salary BETWEEN 10000 AND 20000
ORDER BY salary DESC;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.