I have a data a set which is already grouped by Person and Class columns and I use this query for this process:
SELECT Person,Class, MAX(TimeSpent) as MaxTimeSpent
FROM Persons
GROUP BY Person,Class
Output:
Person Class MaxTimeSpent
--------|--------|-------------|
MJ | 0 | 0 |
MJ | 1 | 659 |
MJ | 2 | 515 |
What I want to do is to get the row that has the maximum Class value in this data set (which is the 3rd row for this example).
How can I do this ? Any help would be appreciated.
TOP 1wont work since i useWHEREcaluse to make easier to test my query.ROW_NUMBER(no ties) orRANK(with ties) to number the rows within each group, ordered byClass, and then to filter out just the Row / Rank = 1 rows.