Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 547727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:00:16+00:00 2026-05-13T11:00:16+00:00

I found a question that was very similar to this one, but using features

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:00:16+00:00Added an answer on May 13, 2026 at 11:00 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I haven't found an answer to this particular question; perhaps there isn't one. But
I searched and found this question but did not like the answer. Is there
I've found a similar question on stack overflow, but it didn't really answer the
I searched for this and found Maudite's question about text editors but they were
Simple question, but I haven't found a good explanation on google. When using Set
I recently asked a question about IIf vs. If and found out that there
I found a question here about blogs on software development, but I would like
I found this question in an old question in your website so i thought
Reading this question I found this as (note the quotation marks) code to solve
I browsed around here on Stackoverflow and found this topic : https://stackoverflow.com/questions/279/aspnet-visual-studio-and-subversion-how-to-integrate However that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.