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 > Database Server Software > DB2 > How to get the highest values per given col

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-13-08, 05:49
swiss01 swiss01 is offline
Registered User
 
Join Date: Jul 2007
Posts: 31
Post How to get the highest values per given col

have 2 tables X and Y in DB2 V8 db

Table X
-----------------------------------
COL S COL V COL S
--------- ------------ ---------
S1 A TT
S1 B TT
S1 C TT
S2 F TT
S3 D TT
S3 Z TT

Table Y
-----------------------------------
COL V COL N
----------- --------------
A 30
B 20
C 60
F 10
D 80
Z 40

Result needs to be 1 row per distinct COL S values and the one with the highest value in COL N . So in above case :
S1 C 60
S2 F 10
S3 D 80


Thanks
Reply With Quote
  #2 (permalink)  
Old 02-13-08, 08:18
swiss01 swiss01 is offline
Registered User
 
Join Date: Jul 2007
Posts: 31
Found it .
select X.S,MAX(Y.N) from X,Y
where X.V = Y.V
group by X.S;
Reply With Quote
  #3 (permalink)  
Old 02-13-08, 08:22
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
with t1 (s,v,n) as
(select x.cols,x.colv,t.coln from x inner join y on (x.colv = y.colv)),
t2 (s,n) as
(select s,max(n) from t1 group by s)
select * from t1 where (s,n) in (select * from t2)

Andy
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