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 

query needed to optimise the process

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


Joined: 12 Aug 2005
Posts: 102
Topics: 19

PostPosted: Fri Sep 15, 2006 8:37 am    Post subject: query needed to optimise the process Reply with quote

can anyone let me know the query to fetch the fields as per the below.

Condition:
For m_num in table A,I need to fetch the fields m_num, q_num, name, q_num-name,date,week as shown in expected result.

Iam not sure how to add another column and correlate the data correspondingly.
Can anyone help me.

Table A
-------
Code:
+------+-------------------------+-------------------------
|m_num |date                     |wk
+------+-------------------------+-------------------------
|242349|07-aug-2006              |13-aug-2006             
|242249|09-aug-2006              |13-aug-2006             
.
.
.


Table B
-------

|
Code:
m_num |q_Num |name                          |compen
+---------+---------+------------------------------+------
|242349|540522|VIRGINIA    KNUE              |D     
|435235|743805|BRADLEY     CERRA             |E     
|396462|689040|IVAN        ADAMS          SR |D     
|234929|446835|KIRA        HAMMERGREN        |D     
|234321|040298|SARAH       WILSON            |E     
|040298|012298|RIKHI       ALSON             |E     
|012298|423498|ADAM        WILLS             |E     


Expected result
---------------
Code:
                                     q_num name 
|m_num    |q_Num    |name           |(q.num=m.num)      |date |week
+---------+---------+---------------|-------------------+------
|234321   |040298   |SARAH  WILSON  | RIKHI  ALSON      |     
|040298   |012298   |RIKHI  ALSON   | ADAM   WILLS       |     
.
.
.
.


Hope I made the query clear.
Back to top
View user's profile Send private message
Bithead
Advanced


Joined: 03 Jan 2003
Posts: 550
Topics: 23
Location: Michigan, USA

PostPosted: Fri Sep 15, 2006 2:27 pm    Post subject: Reply with quote

Try this:
Code:

select a.m_num,
         b1.q_num,
         b1.name,
         b2.name as 'q_num name',
         a.date,
         a.wk as 'week'
from table_a a,
     table_b b1,
     table_b b2
where b1.m_num = a.m_num
and   b2.m_num = b1.q_num
Back to top
View user's profile Send private message
radkrish
Beginner


Joined: 12 Aug 2005
Posts: 102
Topics: 19

PostPosted: Tue Sep 19, 2006 12:44 am    Post subject: Reply with quote

Thanks!.The query extracts data in an efficient way.

One more requirement to add to it.I have to combine another table which is l_table_b (late table-similar to table_b.The difference being the data loads into l_table_b in a week and where as data loads in table_b in daily basis)

Both table_b and l_table_b is similar in structure.Except the data stored in it is different.

I thought of using OR condition in where clause to the above.
the query looks like

Code:
select a.m_num,
         b1.q_num,
         b1.name,
         b2.name as 'q_num name',
         a.date,
         a.wk as 'week'
from table_a a,
     table_b b1,
     table_b b2,
     l_table_b b3,
     l_table_b b4,
where b1.m_num = a.m_num
and   b2.m_num = b1.q_num
or    b3.m_num = a.m_num
or    b4.m_num = b3.q_num

Not sure abt the query written above.
Can anyone help me to acheive a better result/query.
Back to top
View user's profile Send private message
radkrish
Beginner


Joined: 12 Aug 2005
Posts: 102
Topics: 19

PostPosted: Tue Sep 19, 2006 12:56 am    Post subject: Reply with quote

I guess that it can also be used with UNION ALL.But
is there any other possible/better way to acheive it.
Back to top
View user's profile Send private message
Bithead
Advanced


Joined: 03 Jan 2003
Posts: 550
Topics: 23
Location: Michigan, USA

PostPosted: Tue Sep 19, 2006 1:47 pm    Post subject: Reply with quote

I would run a UNION:
Code:

select a.m_num,
         b1.q_num,
         b1.name,
         b2.name as 'q_num name',
         a.date,
         a.wk as 'week'
from table_a a,
     table_b b1,
     table_b b2
where b1.m_num = a.m_num
and   b2.m_num = b1.q_num
UNION
select a.m_num,
         b1.q_num,
         b1.name,
         b2.name as 'q_num name',
         a.date,
         a.wk as 'week'
from table_a a,
     l_table_b b1,
     l_table_b b2,
where b1.m_num = a.m_num
and   b2.m_num = b1.q_num
Back to top
View user's profile Send private message
radkrish
Beginner


Joined: 12 Aug 2005
Posts: 102
Topics: 19

PostPosted: Wed Sep 20, 2006 5:52 am    Post subject: Reply with quote

Thanks Bithead
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