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 > Joining to status table needing MaxID only IF status = 'O'

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-05-04, 15:56
kburns1649 kburns1649 is offline
Registered User
 
Join Date: Nov 2003
Posts: 9
Joining to status table needing MaxID only IF status = 'O'

Joining to a table with multiple rows needing only the max key if status = 'O'.

The problem I am having is when I use Max(STA_ID) and I can't use where clause for STA_CD = 'O'. I only want to join "IF" the MAX(STA_ID) has Status code of 'O'.

Thanks in Advance, Marcel K. Data below would not yield a join, because the MAX(STA_ID) STA_CD = 'C'.


ID_NO STA_ID STA_CD
1085614000006 1999-03-18-23.16.39.945269 C
1085614000006 1999-03-18-11.59.40.284873 O
1085614000006 1999-03-17-10.39.20.630029 O
1085614000006 1999-03-11-06.57.31.405619 O

DB2 Version is 7.1.1
Reply With Quote
  #2 (permalink)  
Old 02-05-04, 16:15
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
I think this is what you want:

Assumptions:
tableA = parent table
TableB as described in your post

select ... from tableA as A, tableB as b
where (a.ID_NO = b.ID_NO) and (b.sta_ID =
(select max(b2.sta_ID) from tableB as B2 where (b2.ID_NO = a.ID_NO)) and (b.STA_CD = 'O')

HTH

Andy
Reply With Quote
  #3 (permalink)  
Old 02-05-04, 16:36
kburns1649 kburns1649 is offline
Registered User
 
Join Date: Nov 2003
Posts: 9
If it helps, table A to table be is a one to many. I tried running the suggestion and I was getting no rows back. I only want to join when the max record is 'O', not the max record with 'O'. Keyword here is IF it has an 'O' as in open.

thanks.
Reply With Quote
  #4 (permalink)  
Old 02-05-04, 16:44
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Woops, I missed a closing parenthesis ")"

try:

select ... from tableA as A, tableB as b
where (a.ID_NO = b.ID_NO) and (b.sta_ID =
(select max(b2.sta_ID) from tableB as B2 where (b2.ID_NO = a.ID_NO))) and (b.STA_CD = 'O')

Andy
Reply With Quote
  #5 (permalink)  
Old 02-05-04, 16:55
kburns1649 kburns1649 is offline
Registered User
 
Join Date: Nov 2003
Posts: 9
Still not flying it doesn't error out, but no data is coming back.

Thanks for your input.
Reply With Quote
  #6 (permalink)  
Old 02-05-04, 17:00
kburns1649 kburns1649 is offline
Registered User
 
Join Date: Nov 2003
Posts: 9
Sample data had 3 columns, not looking like three in initial post.

ID_NO STA_ID STA_CD
1085614000006 1999-03-18-23.16.39.945269 C
1085614000006 1999-03-18-11.59.40.284873 O
1085614000006 1999-03-17-10.39.20.630029 O
1085614000006 1999-03-11-06.57.31.405619 O
Reply With Quote
  #7 (permalink)  
Old 02-05-04, 17:26
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
The way I understand what you want is this. Show the ID_NO if the row in table B with the max(STA_ID) has a STA_CD value of 'O', otherwise do not show th ID_NO. If this is the case, then with the data you have shown, you will get nothing back because the max(sta_cd) has a 'C". Now if you want the 2nd row returned, then the query would be:

select ... from tableA as A, tableB as b
where (a.ID_NO = b.ID_NO) and (b.sta_ID =
(select max(b2.sta_ID) from tableB as B2 where (b2.ID_NO = a.ID_NO) and (b2.STA_CD = 'O'))) and (b.STA_CD = 'O')

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