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 

Outer joins

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


Joined: 14 Jul 2008
Posts: 9
Topics: 1

PostPosted: Tue Jan 25, 2011 6:14 am    Post subject: Outer joins Reply with quote

Hi All,

I surfed a lot on my below requirement, but couldn't get the exact solutions anywhere. Please guide me thru this,

My Req:

I have 3 tables, say tablea,tableb and tablec. Table a and table b have have common key field, table b and table c have common key field. but there is no relation between table a and table c.

i want to count and extract the details based on the age range present in table a.

Code:


select count(*),a.lowerlmt,a.upperlmt,b.security,c.number1
from (
tablea
left outer join
tableb
on
a.type = b.type and
a.age between b.lowerlmt and b.upperlmt
)

left outer join
table c on
b.key1 = c.key

where b.type = 'xxx'

group by a.lowerlmt,a.upperlmt,b.security,c.number1
order by 5,4


table values tablea
Code:


lowerlmt    upperlmt   type
  1                5           xxx
  6                10         xxx
  11              15         xxx
  16              20         xxx


and my o/p is
Code:


count  lowerlmt upperlmt security number1
2           1            5         123       10001
1           10         15         123       10001
3           6           10         1234     10001
0           16          20         -----     -------



expected o/p

Code:


count  lowerlmt upperlmt security number1
2           1            5         123       10001
0           6           10         ----       -------
1           11         15         123       10001
0           16         20        ------      ------
0           1           5          -----       -------
3           6           10         1234     10001
0           11         15         ------      ------
0           16         20        ------      ------


i.e For every age range in tablea, we should get a value in the o/p grouped by number and security.

hope this is clear.

Thanks in advance,
VPV
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


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

PostPosted: Tue Jan 25, 2011 11:37 am    Post subject: Reply with quote

veluvpv,

Your table data does not match the Output values. How did you get a count value of 2 for the range 1 - 5?

Where is the security and number coming from?

Show us complete tables

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
veluvpv
Beginner


Joined: 14 Jul 2008
Posts: 9
Topics: 1

PostPosted: Tue Jan 25, 2011 1:42 pm    Post subject: Reply with quote

Kolusu,

Apologize for the missed info. please find the details below,

tableb has keyfield1 (which is common to tablea),number (FK - Tablec. this is the field common to table b and tablec), age,security.

tableb
Code:


type(keyfield1) num1    age security
XXX                 101         3     123
XXX                 101         3     123
XXX                 101         7     123
XXX                 102        12   1234



tablec
Code:


num1   number1
101      10001
102      10002


tablea
Code:


type  lowerlmt   upperlmt 
XXX   1               5
XXX   6               10
XXX   11             15


my expected output is to count the total no. of rows from tableb based on the age range (lower limit and upper limit from table a) and should be grouped by number1(in tablec) and security.

expected o/p
Code:

count   number1   security  lowerlmt     upperlmt
2         100001      123         1                5
1         100001      123         6                10
0 or ' '  100001      123         11               15
0         100002      1234         1                5
0         100002      1234        6                10
1         100002      1234         11               15


i.e for each age range from the tablea there should be some entry.. which means if we have values in that age range that count will be accumulated else it need to display spaces or any other values.

Thanks,
Veluvpv
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


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

PostPosted: Tue Jan 25, 2011 2:57 pm    Post subject: Reply with quote

veluvpv,

Untested sql but hopefully it should give you the desired results.

Code:

SELECT SUM(CASE WHEN D.AGE BETWEEN A.LOWERLIMIT AND A.UPPERLIMIT
                THEN INT(1) ELSE INT(0) END) AS COUNT               
      ,D.NUMBER1                                                 
      ,D.SECURITY                                               
      ,A.LOWERLIMIT                                             
      ,A.UPPERLIMIT                                             
  FROM your_tableA  A                                               
      ,(SELECT B.SECURITY   
              ,B.AGE       
              ,C.NUMBER1
          FROM your_tableB  B
              ,your_tableC  C         
         WHERE B.NUM1 = C.NUM1) D                               
 GROUP BY D.NUMBER1                                             
         ,D.SECURITY                                             
         ,A.LOWERLIMIT                                           
         ,A.UPPERLIMIT                                           
 ;

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
veluvpv
Beginner


Joined: 14 Jul 2008
Posts: 9
Topics: 1

PostPosted: Thu Jan 27, 2011 6:48 am    Post subject: Reply with quote

Kolusu..!!!

you are REALLY GREAT..!!! 8) Very Happy Smile

it worked out and i got the expected result.

thanks a bunch..!!

Regards,
VPV
Back to top
View user's profile Send private message
veluvpv
Beginner


Joined: 14 Jul 2008
Posts: 9
Topics: 1

PostPosted: Tue Feb 01, 2011 7:57 am    Post subject: Reply with quote

Kolusu,
As said in the previous post, the query worked fine.. Now the question is about performance Sad bonk . I have huge data in the db Evil or Very Mad . So is it possible to fine tune the SQL? Question

Appreciate your help on this regard too.

Regards,
VPV
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


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

PostPosted: Tue Feb 01, 2011 11:24 am    Post subject: Reply with quote

veluvpv wrote:
So is it possible to fine tune the SQL? Question

Appreciate your help on this regard too.

Regards,
VPV


Veluvpv,

Please search before posting.

Check this link

http://www.mvsforums.com/helpboards/viewtopic.php?p=1750#1750

Learn more about 'Explain' here

http://mvsforums.com/helpboards/viewtopic.php?t=587&highlight=explain

Hope this helps...

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
veluvpv
Beginner


Joined: 14 Jul 2008
Posts: 9
Topics: 1

PostPosted: Wed Feb 02, 2011 4:23 am    Post subject: Reply with quote

Kolusu,

Apologize for the mistake. Will search before posting hence forth.

Thanks for the reply.

Regards,
VPV
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