繁体   English   中英

为什么SQL中的max函数返回多个值

[英]Why does max function in sql return multiple values

我想展示薪水最高的球员。

select  max(Salary) as highest_salary, p.[Last name]
from tbl_PlayersTable as p, tbl_team as t
where p.Team = t.TeamID
and TeamID = 1000
Group by p.[Last name]

输出为:

highest_salary  Last Name
   8000          Bosh
   7000          Wade
   6000          James

我只想显示(8000波什,因为他是薪水最高的球员)。

您不需要MAXGROUP BY ,只需将TOP 1ORDER BY Salary DESC 像这样:

select TOP (1) Salary as highest_salary, p.[Last name]
from tbl_PlayersTable as p, tbl_team as t
where p.Team = t.TeamID
 and TeamID = 1000
ORDER BY Salary  DESC

您正在将值分组(请参阅最后的分组依据),因此您的max函数将计算每个组的最大值。 如果要使用绝对最大值,请删除分组。

无需group by甚至max

select  top 1 Salary
,       [Last name]
from    tbl_PlayersTable
where   TeamID = 1000
order by
        salary desc

因为您使用group by p.[Last name]所以查询将为其找到的每个不同的Last name获得max(Salary) 因此,如果要基于所有Last name获得max(Salary) ,则必须删除group by

您将需要采用前1个值

  select TOP (1) Salary as maxsalary, p.[Last name]
   from tbl_PlayersTable as p 
   Inner join  tbl_team as t on  p.Team = t.TeamID
  where TeamID = 1000
  ORDER BY Salary  DESC

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM