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 

Join 2 tables

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


Joined: 22 Feb 2007
Posts: 8
Topics: 3
Location: Roswell, GA

PostPosted: Fri Feb 23, 2007 7:40 am    Post subject: Join 2 tables Reply with quote

Problem: I have to do a select on two tables - one that has to join back to itself. I can't code it right.

Table A looks like this: aa,ab,ac,ad...ay,az
Table B looks like this: aa, bb, bc, bd ...

The result table should look like this:
Code:

aa,
ab,
bb,
bc,
ab(from A row where aa = ay)
ab(from A row where aa = az),
ae,
af,
ag.

I tried a number of selects but can't even get past edit.

First I used a case statement:
Code:

Select A.ab,A.ac,B.bb,B.bc,
 Case
   when ay = ' ' then ' '
   else select ab from c.A where c.aa = a.xy.
 End case
, Case
   when az = ' ' then ' '
   else select ab from c.A where c.aa = a.xz.
 End case
,  ae , af, ag
 from a.A b.B

Then I tried Union All with a subselect on the selected set.
Code:

Select A.ab,A.ac,B.bb,B.bc,ay,az,ae,af from a.A B.b Union all
 


  case   
  if ay null and az null
      Select A.ab,A.ac,B.bb,B.bc,' ' , ' ',ae,af from A A, B B
  end case
  case
  if ay null and az not null
      Select A.ab,A.ac,B.bb,B.bc,' ' ,
      select c.ab from  ' ',ae,af from A A, B B
  end case

etc...
Back to top
View user's profile Send private message Send e-mail
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12372
Topics: 75
Location: San Jose

PostPosted: Fri Feb 23, 2007 7:52 am    Post subject: Reply with quote

Quote:

Table A looks like this: aa,ab,ac,ad...ay,az
Table B looks like this: aa, bb, bc, bd ...



are those columns ? or actual values in the columns?

Post clearly the 2 table definitions and sample input and desired output data

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
mikeleibo
Beginner


Joined: 22 Feb 2007
Posts: 8
Topics: 3
Location: Roswell, GA

PostPosted: Fri Feb 23, 2007 8:23 am    Post subject: Reply with quote

Thanks,

If the rows look like this.

A
Code:

  AA1,AB1,null,null
  AA2,AB2,null,AA3
  AA3,AB3,AA2,null

B
Code:

  AA1,BA1,BB1
  AA2,BA2,BB2
  AA3,BA3,BB3


The result has to look like this.
Code:

AB1,'-','-',BA1,BB1
AB2,AB3,'-',BA2,BB2
AB3,'-',AB2,BA3,BB3
Back to top
View user's profile Send private message Send e-mail
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12372
Topics: 75
Location: San Jose

PostPosted: Fri Feb 23, 2007 9:58 am    Post subject: Reply with quote

Table A has the following columns and denoted X
Code:

COL1
COL2
COL3
COL4


Table B has the following columns and denoted Y
Code:

COL1
COL2
COL3
COL4


The following sql will give you the desired results.

Code:

SELECT X.COL2                                               
      ,CASE WHEN X.COL3 IS NULL AND X.COL4 IS NULL           
            THEN CHAR('-')                                   
            WHEN SUBSTR(X.COL3,1,2) = 'AA' AND X.COL4 IS NULL
            THEN CHAR('-')                                   
            WHEN X.COL3 IS NULL AND SUBSTR(X.COL4,1,2) = 'AA'
            THEN CHAR('AB') || SUBSTR(X.COL4,3,1)           
            ELSE X.COL3 END                                 
      ,CASE WHEN X.COL4 IS NULL AND X.COL3 IS NULL           
            THEN CHAR('-')                                   
            WHEN SUBSTR(X.COL4,1,2) = 'AA' AND X.COL3 IS NULL
            THEN CHAR('-')                                   
            WHEN X.COL4 IS NULL AND SUBSTR(X.COL3,1,2) = 'AA'
            THEN CHAR('AB') || SUBSTR(X.COL3,3,1)           
            ELSE X.COL4 END                                 
      ,Y.COL2                                               
      ,Y.COL3                                               
  FROM Tablea  X                                                 
      ,Tableb  Y                                                 
 WHERE X.COL1   = Y.COL1                                     
 ;                                                           


The output is

Code:

---------+---------+-------
COL2            COL2  COL3
---------+---------+-------
AB1   -    -    BA1   BB1 
AB2   AB3  -    BA2   BB2 
AB3   -    AB2  BA3   BB3 

Hope this helps...

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
mikeleibo
Beginner


Joined: 22 Feb 2007
Posts: 8
Topics: 3
Location: Roswell, GA

PostPosted: Fri Feb 23, 2007 12:20 pm    Post subject: Reply with quote

THX, THIS IS WHAT I'M CODING... WILL IT WORK?
Code:

SELECT DISTINCT
  A.ID, A.NBR, B.NM, B.LOB, A.OPEN, A.CLS
      ,CASE WHEN A.TX IS NULL THEN CHAR('-')
            WHEN A.TX = C.ID THEN C.NBR
       END
      ,CASE WHEN A.FR IS NULL THEN CHAR('-')
            WHEN A.FR = C.ID THEN C.NBR
       END
    , A.TYP_CD , A.SZ_CD
  FROM STR A , HIER B , STR C
  WHERE A.ID = B.ID
Back to top
View user's profile Send private message Send e-mail
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12372
Topics: 75
Location: San Jose

PostPosted: Fri Feb 23, 2007 12:56 pm    Post subject: Reply with quote

mikeleibo wrote:
THX, THIS IS WHAT I'M CODING... WILL IT WORK?


why don't you try and let us know

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
mikeleibo
Beginner


Joined: 22 Feb 2007
Posts: 8
Topics: 3
Location: Roswell, GA

PostPosted: Sat Feb 24, 2007 2:17 pm    Post subject: almost works Reply with quote

There's a third table involved and I wound up with a 5,000,000 row result so I added distinct and it's now down to almost the right result but I am getting duplicate rows, only, when the ay,az are not null and then I get botht the row with null and with the right aa value?
Back to top
View user's profile Send private message Send e-mail
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