Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pulling column values based on max() 1

Status
Not open for further replies.

Azathoth

Technical User
Jul 14, 2003
61
US
I have a table (#result) that looks something like this:

unit_number fda_number deferral_days
1 4 25
2 5 10
3 6 75

I want to find the unit_number and fda_number for the unit with the greatest number of deferral_days. I realize the select statement should look something like:

select @deferral_days = max(deferral_days) from #result

But I'm not sure what to put in the where clause to pull the corresponding unit_number and fda_number values...help please?

 
SELECT TOP 1 unit_number, fda_number, deferral_days
FROM #result
ORDER BY defferal_days DESC

*cLFlaVA
----------------------------
A polar bear walks into a bar and says, "Can I have a ... beer?"
The bartender asks, "What's with the big pause?
 

Code:
SELECT unit_number, fda_number 
FROM #result 
WHERE DeferralDays IN 
	(
		select max(deferral_days) from #result
	)

I used "IN" to allow for more than one row having the same max Deferral days


"Own only what you can carry with you; know language, know countries, know people. Let your memory be your travel bag.
 
I always try to make these things way too complicated...

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top