Solved

Selecting BOTTOM values

Posted on 2008-10-15
6
239 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How can i use insert and update together by merging two tables? 9 30
TSQL convert date to string 4 35
Acces SQL Insert Problem 6 32
SQL 2012 clustering 9 12
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

860 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