选择最高行

xam8gpfp  于 2021-08-09  发布在  Java
关注(0)|答案(4)|浏览(276)

请帮我做以下数据。我应该如何查询它。

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

我需要查询的方式,它显示的名称有最高的使用。下面是一个例子

Name        Speed Tier     Usage
Andrew         200          5000
Luis           100          1000
8qgya5xd

8qgya5xd1#

使用相关子查询:

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

gcmastyq2#

我只想用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
pvabu6sv

pvabu6sv3#

我会坚持到底 MAX 以及 GROUP :

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

sqyvllje4#

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

相关问题