选择最高行

Selecting Highest rows

请帮助我提供以下数据。应该怎么查询呢

Name        Speed Tier     Usage
Andrew         200          5000
Andrew         100          2000
Luis           100          1000

我需要以显示使用率最高的名称的方式进行查询。下面是例子

Name        Speed Tier     Usage
Andrew         200          5000
Luis           100          1000
SELECT TOP 1 Name
    ,Usage
FROM your TABLE
ORDER BY Usage DESC -- if you want the highest

SELECT TOP X Name
    ,Usage
FROM your TABLE
ORDER BY Usage DESC -- if you want to select the top X rows

SELECT Name
    ,MAX(Usage) Usage 
FROM your TABLE
GROUP BY Name -- if you want to know each Name's highest usage 

我会通过 MAXGROUP:

SELECT Name, Max(Usage)
FROM YourTableName
GROUP BY Name

我会结合使用 MAX 和 GROUP BY

SELECT Name, Max([Speed Tier]), Max(Usage)
FROM TableName
GROUP BY Name
ORDER BY Name

或者如果你只想要一个价值最高的人

SELECT TOP 1 Name, [Speed Tier], Usage
FROM TableName
GROUP BY Name
ORDER BY [Usage] desc

或者如果您想要最高的使用率以及当前行的速度

;with CTE as (SELECT TOP 1 Name, Max(Usage) as Usage
FROM @mytable
GROUP BY Name
Order by Usage desc
)
select 
    T.name, T.[Speed Tier], T.usage
from cte as C
inner join @mytable as T on T.Name = C.name and T.usage = C.Usage

使用相关子查询:

select t.*
from t
where t.usage = (select max(t2.usage) from t t2 where t2.name = t.name);