Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Select second highest wage


Select second highest wage

Author
Message
luckysql.kinda
luckysql.kinda
Old Hand
Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)

Group: General Forum Members
Points: 314 Visits: 659
select * from EmpDetails

EmpID Empname ManagerId EmpWage
1001 Lucky101 1005 1000.00
1002 Lucky102 1005 2000.00
1003 Lucky103 1005 3000.00
1004 Lucky104 1005 4000.00
1005 Lucky105 1010 5000.00
1006 Lucky106 1010 6000.00


I need to select second last Empwage here.

----------------
SELECT TOP 1 EmpID, EmpWage
FROM Empdetails
WHERE Empwage <>
(SELECT TOP 1 EmpWage FROM EmpDetails
ORDER BY Empwage
DESC
)
ORDER BY Empwage
DESC
-------------------
This code works. But how to get fourth last Empwage ie Lucky103;3000? Is there any simple code here?

-LK
matt6288
matt6288
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1208 Visits: 1350
This is a simple way to start. You might have to adjust the logic to determine what row you want to be returned.


CREATE TABLE #temp (empid INT, empname VARCHAR(50), managerID INT, empWage MONEY)

INSERT INTO #temp
SELECT 1001, 'Lucky101', 1005, 1000.00
UNION ALL
SELECT 1002, 'Lucky102', 1005, 2000.00
UNION ALL
SELECT 1003, 'Lucky103', 1005, 3000.00
UNION ALL
SELECT 1004, 'Lucky104', 1005, 4000.00
UNION ALL
SELECT 1005, 'Lucky105', 1010, 5000.00
UNION ALL
SELECT 1006, 'Lucky106', 1010, 6000.00



;WITH CTE AS
(
SELECT *, ROW_NUMBER() OVER (ORDER BY empWage desc) AS rowNum
FROM #temp
)

SELECT * FROM cte
WHERE rowNum = 4

DROP TABLE #temp


luckysql.kinda
luckysql.kinda
Old Hand
Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)

Group: General Forum Members
Points: 314 Visits: 659
Hey Matt Wilhoite,

You could do this because you know the row number. Take a scenario where we have a lot of data and we don't know which row number we want.

You need to simply give me the sixth highest Empwage. I think I am understood this time.

-Lucky
SQLGeek-652125
SQLGeek-652125
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 10
Here is a very simple way to achieve what you are looking for.


SELECT TOP 1 empid,empWage
FROM
(
SELECT TOP 4 empid,empWage
FROM Empdetails
ORDER BY empid DESC
) a
ORDER BY a.empid ASC


luckysql.kinda
luckysql.kinda
Old Hand
Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)

Group: General Forum Members
Points: 314 Visits: 659
Correct. Instead of ORDER BY empid DESC you should use ORDER BY empwage DESC.

Thanks anyway.

-Lk
matt6288
matt6288
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1208 Visits: 1350
The rowNumber is determined based off the row_number() function and it will be in the order of whatever is specified by the order by clause of that function. So yes, you asked for the 4th empWage so that is why the rowNum = 4. If you want the 6th then the rowNum would equal 6. Check out the row_number function in books online to see if it will do what you are needing.

EDIT:

Sorry just reread your earlier post. You say you won't know the wage you will want but then ask for the 6th highest. I am confused as to how you will implement this. You could use a variable in the rowNum = 4 line. Use something like rowNum = @variable. Would that work?
luckysql.kinda
luckysql.kinda
Old Hand
Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)

Group: General Forum Members
Points: 314 Visits: 659
Good suggestion Matt Thanks.

-LK
jerry-621596
jerry-621596
Old Hand
Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)

Group: General Forum Members
Points: 373 Visits: 644
I got it with this:

SELECT TOP 1 EmpID, MAX(EmpWage) AS [Max Wage]
FROM EmpDetails
GROUP BY EmpID
HAVING MAX(EmpWage) < (SELECT TOP 1 MAX(EmpWage) FROM EmpDetails)
ORDER BY [Max Wage] DESC


WayneS
WayneS
SSCertifiable
SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)

Group: General Forum Members
Points: 6253 Visits: 10403
And what happens if the bottom 5 people have the worse wage, and what you want is the 6th person (2nd lowest wage)? You might want to use the RANK() function.
declare @TopWagePosition int;
set @TopWagePosition = 2;

WITH CTE AS
(
SELECT EmpID,
EmpWage,
RN = RANK() OVER (ORDER BY EmpWage DESC)
FROM EmpDetails
)
SELECT *
FROM CTE
WHERE RN = @TopWagePosition;



You might want to see this article for how all of the Windowing Functions work: SQL Server Ranking Functions

Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings

Subbu S
Subbu S
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 138
Select * From #temp t1 Where
(2-1) = (Select Count(Distinct(t2.empWage)) From #temp t2 Where
t2.empWage > t1.empWage)

The innery query will return a refernce to the current row.


Genreal Format will be,

Select * From #temp t1 Where
(N-1) = (Select Count(Distinct(t2.empWage)) From #temp t2 Where
t2.empWage > t1.empWage)

The N should be replaced the by the number(Rank) which u hav to find.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search