Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch the row which has the Max value for a column in SQL Server

I found a question that was very similar to this one, but using features that seem exclusive to Oracle. I'm looking to do this in SQL Server.

I have a table like this:

MyTable
--------------------
MyTableID  INT  PK
UserID     INT
Counter    INT

Each user can have multiple rows, with different values for Counter in each row. I need to find the rows with the highest Counter value for each user.

How can I do this in SQL Server 2005?

The best I can come up with is a query the returns the MAX(Counter) for each UserID, but I need the entire row because of other data in this table not shown in my table definition for simplicity's sake.

EDIT: It has come to my attention from some of the answers in this post, that I forgot an important detail. It is possible to have 2+ rows where a UserID can have the same MAX counter value. Example below updated for what the expected data/output should be.

With this data:

MyTableID   UserID   Counter
---------   -------  --------
1           1         4
2           1         7
3           4         3
4           11        9
5           11        3
6           4         6
...
9           11        9

I want these results for the duplicate MAX values, select the first occurance in whatever order SQL server selects them. Which rows are returned isn't important in this case as long as the UserID/Counter pairs are distinct:

MyTableID   UserID    Counter
---------   -------   --------
2           1         7
4           11        9
6           4         6
like image 432
Dan Herbert Avatar asked Jan 19 '10 22:01

Dan Herbert


People also ask

How do I get the maximum row value in SQL?

We used the MAX() function within a subquery to find the maximum value, and returned the whole row with the outer query.

How do I get the max value in a column in SQL?

The MAX() function returns the largest value of the selected column.

How do I get the maximum value from another column of a table in SQL?

The MySQL Solution If you're working with MySQL, you can combine MAX() with the GREATEST() function to get the biggest value from two or more fields. Here's the syntax for GREATEST: GREATEST(value1,value2,...)

How do I find the maximum value of multiple rows in SQL?

To find the maximum value of a column, use the MAX() aggregate function; it takes a column name or an expression to find the maximum value. In our example, the subquery returns the highest number in the column grade (subquery: SELECT MAX(grade) FROM student ).


2 Answers

I like to use a Common Table Expression for that case, with a suitable ROW_NUMBER() function in it:

WITH MaxPerUser AS
(
  SELECT 
    MyTableID, UserID, Counter,
    ROW_NUMBER() OVER(PARTITION BY userid ORDER BY Counter DESC) AS 'RowNumber'
  FROM dbo.MyTable
)
SELECT MyTableID, UserID, Counter 
FROM MaxPerUser
WHERE RowNumber = 1

THat partitions the data over the UserID, orders it by Counter (descending) for each user, and then labels each of the rows starting with 1 for each user. Select only those rows with a 1 for rownumber and you have your max. values per user.

It's that easy :-) And I get results something like this:

MyTableID   UserID  Counter  
   2           1        7   
   6           4        6
   4          11        9

Only one entry per user, no matter how many rows per user happen to have the same max value.

like image 98
marc_s Avatar answered Sep 24 '22 15:09

marc_s


I think this will help you.

SELECT distinct(a.userid), MAX(a.counterid) as counterid 
FROM    mytable a INNER JOIN mytable b ON a.mytableid = b.mytableid 
GROUP BY a.userid
like image 38
shiva rao Avatar answered Sep 26 '22 15:09

shiva rao