MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

SQL Select when more rows and single Rows

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
DGM
Beginner


Joined: 21 Sep 2003
Posts: 12
Topics: 4

PostPosted: Thu Nov 24, 2005 8:47 pm    Post subject: SQL Select when more rows and single Rows Reply with quote

Hi
I need to have a SQL based on inner select.
I want to get the Latest PO information when multiple occurance is present in table.

Code:

 TableA
Col1     Col2   Col3   
PO       Loc     ORD_DT

TableB
Col1     Col2     col3
PO       SEQ      Status



TableA Key (PO,LOC)
PO    LOC     ORD_DT
11     1      2005-01-02
11     10     2005-05-02
11     111    2005-11-02
21     2      2005-02-01
21     20     2005-02-02
21     222    2005-11-03
31     3      2005-11-05

TableB( KEY PO, SEQ)
PO    SEQ    STATUS
11    1      A
11    2      B
11    3      C
21    1      A
21    2      B
21    3      C
31    1      A


OUTPUT
PO   LOC  SEQ    STATUS  ORD_DT
11   111   3      P      2005-11-02
21   222   3      P      2005-11-03
31   3     1       A      2005-11-05

If the PO occurs multiple times then I want to have status as P and Sequence as maximum sequence,
Latest Order date otherwise I want the actual value from table.
Back to top
View user's profile Send private message Send e-mail
videlord
Beginner


Joined: 09 Dec 2004
Posts: 147
Topics: 19

PostPosted: Fri Nov 25, 2005 4:15 am    Post subject: Reply with quote

Try the following SQL (tested in DB2 for z/OS V8 ):

Code:

  WITH Y as (SELECT PO, MAX(SEQ) as max_seq, count(*) as num1
   from tableb group by po)
  select a.PO, a.LOC, B.B_SEQ, B.B_STATUS, A.ORD_DT
    FROM tablea as A,
     (select Y.PO as B_PO, Y.MAX_SEQ as B_SEQ,
       case when num1 = 1 then X.status else 'P' end as b_status
       from tableb as X, Y
       where (X.PO, X.SEQ) = (Y.PO, Y.MAX_SEQ) ) as B
    where A.PO = B.B_PO
      and (A.PO, A.LOC) in
          (select PO, MAX(LOC) from tablea group by PO)
Back to top
View user's profile Send private message
DGM
Beginner


Joined: 21 Sep 2003
Posts: 12
Topics: 4

PostPosted: Sat Nov 26, 2005 12:12 pm    Post subject: Reply with quote

Hi
Thanks for the post.
Can you please explain me the following SQL.

select Y.PO as B_PO, Y.MAX_SEQ as B_SEQ,
case when num1 = 1 then X.status else 'P' end as b_status
from tableb as X, Y
where (X.PO, X.SEQ) = (Y.PO, Y.MAX_SEQ) ) as B
where A.PO = B.B_PO
Back to top
View user's profile Send private message Send e-mail
videlord
Beginner


Joined: 09 Dec 2004
Posts: 147
Topics: 19

PostPosted: Mon Nov 28, 2005 12:13 am    Post subject: Reply with quote

result B store data with 3 column3: PO, B_SEQ, B_STATUS.
each different PO from tableb will generate one record in B, with the max SEQ of the PO, and B_STATUS will be 'P' if there is more than 1 record for the PO in table b, or it will be the status of the record if there is only 1 record for the PO
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group