Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Old 11-05-2006, 02:11 PM Grouping question
Junior Talker

Posts: 1
Name: Dwight
Trades: 0
A simple query I can’t get my brain around:

I have a table that looks like this:

Col1 Col2 Col3
1 a 1
1 b 2
1 c 3

Assume there are many more rows with different values for all columns. I want to return Col1 and Col2 based on the max value of Col3 grouped by Col1. I don’t want to aggregate Col2 but just want the value that corresponds to the aggregate max of Col3.

“SELECT Col1, Col2, MAX(Col3) GROUP BY Col1, Col2” doesn’t give me what I want and “SELECT Col1, Col2, MAX(Col3) GROUP BY Col1” isn’t allowed because Col2 isn’t aggregated or listed in the GROUP BY clause. How do I get the values of Col1 and Col2 based on the max of Col3 for the group of Col1?
JDTWorld is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-05-2006, 06:13 PM Re: Grouping question
Extreme Talker

Posts: 246
Trades: 3
Maybe this is what you want:

SELECT a.Col1, b.Col2, MAX(a.Col3) FROM mytable a, mytable b WHERE a.Col1 = b.Col1 and a.Col3 = b.Col3 GROUP BY a.Col1
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by CouponGuy; 11-05-2006 at 06:23 PM..
CouponGuy is offline
Reply With Quote
View Public Profile
 
Old 11-06-2006, 03:00 AM Re: Grouping question
Average Talker

Latest Blog Post:
5000 Web Directories
Posts: 15
Trades: 0
try this, if you're server supports subqueries, making a left join to a subquery result.

select a.col1,a.col2,b.maxCol3 from mytable a
left join (select a2.col1,max(a2.col3) as maxCol3
from mytable a2 group by a2.col1) b on a.col1 = b.col1
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
j4s0n is offline
Reply With Quote
View Public Profile Visit j4s0n's homepage!
 
Reply     « Reply to Grouping question
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.12157 seconds with 12 queries