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 > Fetching Data from Different table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-03-11, 07:32
DBforumnewbie DBforumnewbie is offline
Registered User
 
Join Date: Jul 2011
Posts: 11
Fetching Data from Different table

Fetching Data from Different table
TABLE1
ID Name course Code
1 Jo WBT 11
2 saha ICA 22
3 Asw BCT 33


TABLE 2
ID Score Course Code
2 100 ABT 22
5 300 BTA 33
3 200 CBA 33
6 50 NGT 33

Query the table using code = 33
RESULT TABLE
ID Name Course Score Code
3 asw BCT 33
3 CBA 200 33
5 BTA 300 33
6 NGT 50 33


is it possible(Appending 2 table records row vise) ?
Reply With Quote
  #2 (permalink)  
Old 08-03-11, 07:38
rameshj6 rameshj6 is offline
Registered User
 
Join Date: Oct 2008
Posts: 51
Arrow

Use joins....
Reply With Quote
  #3 (permalink)  
Old 08-03-11, 08:14
prakash56 prakash56 is offline
Registered User
 
Join Date: Jan 2004
Location: UK
Posts: 11
select * from tab1

A B C D
---------- ---------- ---------- ----------
2 100 ABT 22
5 300 BTA 33
3 200 CBA 33
6 50 NGT 33

select * from tab2

A B C D
---------- ---------- ---------- ----------
3 asw BCT 33
3 CBA 200 33
5 BTA 300 33
6 NGT 50 33


SELECT TAB1.*
FROM TAB1 inner JOIN TAB2
ON TAB1.A = TAB2.A
where tab2.D='33'"
order by 1
A B C D
---------- ---------- ---------- ----------
3 200 CBA 33
3 200 CBA 33
5 300 BTA 33
6 50 NGT 33
Reply With Quote
  #4 (permalink)  
Old 08-03-11, 18:52
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Compared the column names and values of the result with those of table1 and table2,
I thought that the result might be like...
Code:
ID Name Course Score Code
3  asw  BCT          33
3       CBA      200 33
5       BTA      300 33
6       NGT       50 33
If so, union all may be an answer.

Example 1:
Code:
------------------------------ Commands Entered ------------------------------
WITH
 TABLE1(ID , Name , course , Code) AS (
VALUES
  (1 , 'Jo'   , 'WBT' , '11')
, (2 , 'saha' , 'ICA' , '22')
, (3 , 'Asw'  , 'BCT' , '33')
)
, TABLE2(ID , Score , Course , Code) AS (
VALUES
  (2 , 100 , 'ABT' , '22')
, (5 , 300 , 'BTA' , '33')
, (3 , 200 , 'CBA' , '33')
, (6 ,  50 , 'NGT' , '33')
)
(
SELECT id
     , name
     , course
     , CAST(NULL AS INT) AS score
     , code
 FROM  table1
 WHERE code = 33
UNION ALL
SELECT id
     , CAST(NULL AS VARCHAR(3) ) AS name
     , course
     , score
     , code
 FROM  table2
 WHERE code = 33
)
 ORDER BY
       id
     , name
;
------------------------------------------------------------------------------

ID          NAME COURSE SCORE       CODE
----------- ---- ------ ----------- ----
          3 Asw  BCT              - 33  
          3 -    CBA            200 33  
          5 -    BTA            300 33  
          6 -    NGT             50 33  

  4 record(s) selected.

Generally speaking, an UNION ALL can be replaced by a FULL OUTER JOIN
(though, it may be not so efficient than union all),
like this...

Example 2:
Code:
SELECT COALESCE(t1.id     , t2.id    ) AS id
     , t1.name
     , COALESCE(t1.course , t2.course) AS course
     , t2.score
     , COALESCE(t1.code   , t2.code  ) AS code
 FROM  table1 t1
 FULL  OUTER JOIN
       table2 t2
   ON  0 <> 0
 WHERE t1.code = 33
   OR  t2.code = 33
/*     33 IN (t1.code , t2.code) */
 ORDER BY
       t1.id
     , t2.id
;
------------------------------------------------------------------------------

ID          NAME COURSE SCORE       CODE
----------- ---- ------ ----------- ----
          3 Asw  BCT              - 33  
          3 -    CBA            200 33  
          5 -    BTA            300 33  
          6 -    NGT             50 33  

  4 record(s) selected.

Last edited by tonkuma; 08-03-11 at 20:18. Reason: Change sequences of columns in COALESCE in Example 2. Add alternate condition in comment in Example 2.
Reply With Quote
  #5 (permalink)  
Old 08-04-11, 04:50
DBforumnewbie DBforumnewbie is offline
Registered User
 
Join Date: Jul 2011
Posts: 11
Thank u So mcuh Tokuma !!!!
It solved my Purpose !!!!
Reply With Quote
  #6 (permalink)  
Old 08-04-11, 08:32
DBforumnewbie DBforumnewbie is offline
Registered User
 
Join Date: Jul 2011
Posts: 11
Thank u for all who took so much to reply me
Reply With Quote
Reply

Tags
fetch data

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