SQL : 获取一列中的最小值和最大值

SQL : get Min and Max value in one column

我有一个 employee table 有名字和工资。

我想打印这 2 列,其中只有 2 条记录,即我收入最高和最低的员工的姓名。

它应该看起来像这样:

Name      Salary  
------------------
James       800  
Samanth    3000

下面的代码不是我想要的,我想要 2 列中的最小值和最大值,每个值有 2 个名称

SELECT 
    name, MIN(salary), MAX(salary)
FROM
    employee

有多种方法可以做到这一点。这只是其中之一。如果他们有相同的 min/max 薪水

,它也可能给出超过 2 条记录

声明@min int, @max int select @min=min(工资),@max=max(工资) 来自 YourTable

SELECT 姓名,来自 YourTable 的工资 where Salary=@min OR Salary=@max

我用的就是这种方式。你可以这样做

DECLARE @minsalary varchar(60)
set @minsalary = (select top 1 concat(Name, ' ', salary) from employee where salary= (select min(salary) from employee) 
)

DECLARE @maxsalary varchar(60)
set @maxsalary = (select top 1 concat(Name, ' ', salary) from employee where salary= (select max(salary) from employee) 
)

select concat(@maxsalary, ' ', @minsalary)

最佳方法取决于数据库,但以下使用标准 SQL:

select max(case when seqnum_asc = 1 then salary end) as min_salary,
       max(case when seqnum_asc = 1 then name end) as min_salary_name,
       max(case when seqnum_desc = 1 then salary end) as max_salary,
       max(case when seqnum_desc = 1 then name end) as max_salary_name      
from (select e.*,
             row_number() over (order by salary asc) as seqnum_asc,
             row_number() over (order by salary desc) as seqnum_desc
      from employee e
     ) e;

如果您正在使用 mysql

,您可以执行以下操作
  1. 按薪水第 1 分组所有用户
  2. 使用'order by limit 1'
  3. 最低工资的用户列表
  4. 与第 2 步相同以获得最高薪水
  5. 联合他们

即使超过 1 个人有 max/min 薪水

,这也能解决问题
SELECT * 
FROM   ( 
                SELECT   group_concat(NAME) AS names , 
                         salary , 
                         'minimum' AS remarks 
                FROM     employee 
                GROUP BY salary 
                ORDER BY salary limit 1)tmp 
UNION ALL 
SELECT * 
FROM   ( 
                SELECT   group_concat(NAME) AS names , 
                         salary , 
                         'maximum' AS remarks 
                FROM     employee 
                GROUP BY salary 
                ORDER BY salary DESC limit 1
    )tmp1

示例输出

names       salary  remarks
tom,jolly   10      minimum
sally     10001   maximum

你可以试试这个。示例输出。

name      MAX(salary)   MIN(salary)
George    9200          9200
James     5000           100
Kanye     3200          3200
Nicole    4500          4500
Samanth   3000          2300
Umut      1500           250
Vladimir  2300           330

示例table 值

id  name      salary
 1  James        800
 2  Samanth     3000
 3  Umut        1500
 4  Umut         250
 5  Nicole      4500
 6  George      9200
 7  Kanye       3200
 8  Vladimir    2300
 9  Vladimir    1000
10  Vladimir     330
11  James       5000
12  James        100
13  Samanth     2300

SELECT name, MAX(salary), MIN(salary) FROM employee GROUP BY name;

我终于想出了一个简单的代码来满足我的需求。

select emp_name, salary
from employees
where salary = (select max(salary) from employees)
union all
select emp_name, salary
from employees
where salary = (select min(salary) from employees);

我不知道 Union。 谢谢大家的贡献

您可以使用子查询来做到这一点:

SELECT first_name, salary FROM employees
WHERE salary IN ((SELECT MAX (salary) FROM employees), (SELECT MIN (salary) FROM employees))