Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 selecting rows of maximum column field

Author  Topic 

yishagerew
Starting Member

1 Post

Posted - 2012-03-28 : 14:15:45
I have the following table values
name | id | salary
x | 1 | 10
t | 1 | 11
g | 2 | 12
What i want is to get unique id's of maximum salary value only.the output for the above table will have the following result.
name | id | salary
x | 1 | 11
g | 2 | 12
Any one?

Share of what you know and be happy!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-28 : 14:41:29
multiple ways

Method 1

SELECT t.*
FROM table t
INNER JOIN (SELECT id,MAX(salary) as MaxSal
FROM table
GROUP BY id
)t1
On t1.id = t.id
AND t1.MaxSal = t.salary

Method 2

SELECT name,id,salary
FROM (SELECT *,
ROW_NUMBER() OVER (PARTITION BY id ORDER BY salary DESC) AS rn
FROM table
)t
WHERE Rn=1

Method 3

SELECT t.*
FROM table t
CROSS APPLY (SELECT MAX(Salary) AS MaxSal
FROM table
WHERE id = id
)t1
WHERE t.salary = t1.MaxSal


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

jj735301
Starting Member

3 Posts

Posted - 2012-03-28 : 15:59:16
Wow! Good stuff.

Can you help me with a query to derive:

4|x|12|10
3|t|11|10

from:
x|12
x|10
x|9
x|8
t|11
t|10
t|9

I need the cnt,id,max,next max

Hope that makes sense

Thank you
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-28 : 16:08:02
quote:
Originally posted by jj735301

Wow! Good stuff.

Can you help me with a query to derive:

4|x|12|10
3|t|11|10

from:
x|12
x|10
x|9
x|8
t|11
t|10
t|9

I need the cnt,id,max,next max

Hope that makes sense

Thank you


not much for me

two things

1. please post this as a new thread rather than hijacking an existing one

2. Please explain your output ie rules for getting output from given sample data

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -