Avatar of Sriv
Sriv
 asked on

First Max value

I have below scenario and in SQL i want to display ID of first max value. I want to display 5 because it has first max value

ID   Value
1      10
2       20
3       30
4       40
5       50
6       50
7       50
Microsoft SQL Server

Avatar of undefined
Last Comment
Mike Eghtebas

8/22/2022 - Mon
Jim Horn

Give this a whirl..
SELECT Min(ID)
FROM YourTable 
WHERE Value = (SELECT MAX(Value) as value_max FROM YourTable) 

Open in new window

ASKER CERTIFIED SOLUTION
Scott Pletcher

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Mike Eghtebas

Test table, solution and the result:
--use ee
declare @FirsMaxtbl table(ID int, [Value] int);

Insert into @FirsMaxtbl(ID, [Value]) Values
(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 50), (7, 50);

--Solution:
SELECT  top 1 ID, Value as FirstMaxValue
FROM @FirsMaxtbl Order by Value Desc, ID;

--Result:
ID  FirstMaxValue
5	    50

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61