Group By and it's Highest Value using SQL Query

Malam Malam 266 Reputation points
2025-04-16T17:41:44.86+00:00

In the table below, how do I group by Unit and then get the highest value from Quantity of that group? I have put 21 in row 4 under HighestValue column since its the highest value among all records in Unit

User's image

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,705 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 121.3K Reputation points
    2025-04-17T14:04:36.3633333+00:00

    Check the queries that produce results in different forms:

    select *, max(Quantity) over (partition by Unit) as HighestValue
    from MyTable
    
    select Unit, max(Quantity) as HighestValue
    from MyTable
    group by Unit
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.