If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > the rows with max Val_1 for each Name (was "SQL Querry help")

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-17-06, 01:19
tingcomic tingcomic is offline
Registered User
 
Join Date: May 2006
Posts: 1
Question the rows with max Val_1 for each Name (was "SQL Querry help")

I have table like this (Using SQL Server) :


TempTbl:

ID Name Val_1 Val_2
===============================
1 A 10 adf
2 B 12 dasfasd
3 C 5 fgsdfg
4 A 21 dsfg
5 C 13 sdbs



I want to result a table showing all col with max value in Val_1 for each Name. i.e.

ID Name Val_1 Val_2
===============================
2 B 12 dasfasd
4 A 21 dsfg
5 C 13 sdbs



I can only think of this SQL:

SELECT * FROM TempTbl t1, (SELECT Name, max(val_1) FROM TempTbl Group By Name) t2
WHERE t1.Name = t2.Name AND t1.Val_1 = t2.Val_1



I would like to ask is there any better way for searching this result rather than
joinning the itself twice (becuase the TempTbl is very big).


Many Thanks.!!!!
Reply With Quote
  #2 (permalink)  
Old 05-17-06, 09:27
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
select * 
  from TempTbl as T
 where Val_1
     = ( select max(Val_1) 
           from TempTbl 
          where Name = T.Name )
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On