Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Selecting BOTTOM values

Posted on 2008-10-15
6
Medium Priority
?
255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 200 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

636 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