Solved

Selecting BOTTOM values

Posted on 2008-10-15
6
236 Views
Last Modified: 2012-06-27
I have the following data in a table

COL1          COL2          COL3
--------         --------        --------
1                 2000           2
1                 2001           1
1                 2002           4
1                 2003           3
2                 2000           1
2                 2001           2
2                 2002           4
2                 2003           3

I need to select the records that had the maximum values of COL3 for each COL1
In other words I need to select the records with the values 1,2002,4 AND 2,2002,4

How would I go about this?
0
Comment
Question by:acsakany
  • 2
  • 2
  • 2
6 Comments
 
LVL 13

Expert Comment

by:devsolns
ID: 22722495
select * from tbl where [COL3] = (select max([COL3]) from tbl)
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22722529
select a.* from YourTable a
join (select col1, max(col3) mcol3 from YourTable) b
on a.col1=b.col1
and a.col2=b.col2


or


select * from yourtable a
where col3 = (select max(col3) from yourtable where col1 = a.col1)
0
 

Author Comment

by:acsakany
ID: 22722651
Wouldn't both of these solutions only return one record? I need the MAX for COL3 each time COL1 changes.
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 50 total points
ID: 22722666
No.  Each one will return the max for each col1.

So for your example above:

COL1          COL2          COL3
--------         --------        --------
1                 2002           4
2                 2002           4

0
 
LVL 13

Expert Comment

by:devsolns
ID: 22722740
Nope each will return the max of the column
0
 

Author Closing Comment

by:acsakany
ID: 31506352
Yes you are right. Thank you very much.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now