Selecting all tuples with maximum value for an attribute

Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2007
Posts: 27
Reputation: kartik14 is an unknown quantity at this point 
Solved Threads: 0
kartik14's Avatar
kartik14 kartik14 is offline Offline
Light Poster

Selecting all tuples with maximum value for an attribute

 
0
  #1
Oct 12th, 2008
Hi,

I have a derived attribute in my table and I'm sorting the table in the descending order of this attribute. My query should retrieve all the tuples which have the maximum value for this derived attribute.

If I use SELECT TOP 1, I'm only getting one of the required results. How can I get all the tuples with the maximum value ??

Thanks in advance..

EDIT*: The only problem I'm having in implementing this query is not being able to refer to the derived attribute in the WHERE clause. Can anyone tell me how it's done ?
Last edited by kartik14; Oct 12th, 2008 at 2:24 am.
Copy from one, its Plagiarism; Copy from many, its Research
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Selecting all tuples with maximum value for an attribute

 
0
  #2
Oct 21st, 2008
  1. SELECT * FROM (SELECT * FROM TABLE t1 WHERE t1.COLUMN=value) AS t2
  2. WHERE t2.COLUMN=value
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 149
Reputation: Geek-Master is an unknown quantity at this point 
Solved Threads: 6
Geek-Master's Avatar
Geek-Master Geek-Master is offline Offline
Junior Poster

Re: Selecting all tuples with maximum value for an attribute

 
0
  #3
Oct 25th, 2008
I'm not sure if this will help your case but from what you are asking it sounds like you want to group records based on an attribute and determine the maximum value of each attribute.

If you have a table like the one in this definition

  1. CREATE TABLE dbo.StateAndCountyPopulation
  2. (
  3. CountyID int NOT NULL,
  4. CountyName varchar(50) NOT NULL,
  5. State char(2) NOT NULL,
  6. CountyPopulation INT NOT NULL
  7. )

So if you wanted to see a list of States and their largest County population, you would have to group by the State code and use the aggregate function MAX to determine the highest value of County Population in the code example below.

  1. SELECT
  2. State,
  3. MAX(CountyPopulation)
  4. FROM dbo.StateAndCountyPopulation
  5. GROUP BY State

Hope that helps
If in doubt, reach into the trash can and remove the user guide.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the MS SQL Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC