且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从列表结果中获取最大值

更新时间:2022-03-08 09:39:12

由于您还希望正确的id指向最大值,因此这是最简单的方法:

Since you also want the correct id pointing to the maximum value, this is the easiest way:

WITH TT AS (
    SELECT ID, Name, Number, rank() OVER(PARTITION BY Name ORDER BY Number DESC) R
    FROM employee
)
SELECT ID, Name, Number
FROM TT
WHERE R = 1;

它应该在Oracle和SQL Server上正常工作,因为它们都支持窗口功能.

It should work correctly on Oracle and SQL Server as they both support window functions.

使用Oracle的SQLFiddle示例