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 > Join two identically structured views

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-02-08, 08:03
mah01 mah01 is offline
Registered User
 
Join Date: Sep 2008
Posts: 5
Join two identically structured views

Hi

I have two views that contain the same columns of the same types. They also have the same amount of rows with the same primary keys.

E.g.
Code:
VIEW_A
ID	VALUE
1	(null)
2	1001
3	(null)

VIEW_B
ID	VALUE
1	81007941
2	(null)
3	(null)
I would like to create a third view VIEW_COMBO that combines the two views, getting as many values as possible. I.e. getting the value 81007941 from VIEW_B and the value 1001 from VIEW_A and (null) for the third row where no value exists.

E.g.
Code:
VIEW_COMBO
ID	VALUE
1	81007941
2	1001
3	(null)
Is this possible somehow to write a sql query that does that?

Best regards,
Mattias
Reply With Quote
  #2 (permalink)  
Old 09-02-08, 08:11
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Look at UNION and UNION ALL in the documentation.

Andy
Reply With Quote
  #3 (permalink)  
Old 09-02-08, 08:38
mah01 mah01 is offline
Registered User
 
Join Date: Sep 2008
Posts: 5
Thanks for your suggestion. I've checked the documentation on UNION ALL and tried the following:

Code:
CREATE VIEW VIEW_COMBO AS 
SELECT * FROM VIEW_A 
UNION ALL 
SELECT * FROM VIEW_B
WITH ROW MOVEMENT;
The view is created and contains the following:
Code:
SELECT * FROM VIEW_COMBO;
ID	VAL
1	(null)
2	2000
3	(null)
1	1000
2	(null)
3	(null)
So now I get allt the rows in the new view, but this is not really what I wanted. I wanted it to end up like this:
Code:
ID	VAL
1	1000
2	2000
3	(null)
Is that possible?

Best regards, Mattias
Reply With Quote
  #4 (permalink)  
Old 09-02-08, 08:44
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You need to use UNION instead of UNION ALL if you want to eliminate duplicate rows.

But I believe that you have semantics like this: For each ID value, use the VAL value that is not NULL. Right? What happens if the same ID value occurs in both tables and the VAL value is not NULL in both? Which one do you pick? If we can just take an arbitrary one (or the one from the first table), this would do - untested:
Code:
SELECT COALESCE(t1.id, t2.id), COALESCE(t1.val, t2.val)
FROM   t1 LEFT OUTER JOIN t2 ON t1.id = t2.id
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #5 (permalink)  
Old 09-02-08, 08:50
mah01 mah01 is offline
Registered User
 
Join Date: Sep 2008
Posts: 5
Quote:
Originally Posted by stolze
You need to use UNION instead of UNION ALL if you want to eliminate duplicate rows.

But I believe that you have semantics like this: For each ID value, use the VAL value that is not NULL. Right? What happens if the same ID value occurs in both tables and the VAL value is not NULL in both? Which one do you pick? If we can just take an arbitrary one (or the one from the first table), this would do - untested:
Code:
SELECT COALESCE(t1.id, t2.id), COALESCE(t1.val, t2.val)
FROM   t1 LEFT OUTER JOIN t2 ON t1.id = t2.id
Yes! That's exactly what I want to do, and your suggestion works like a charm Problem solved! Thank you very much!

Best regards,
Mattias
Reply With Quote
  #6 (permalink)  
Old 09-02-08, 09:53
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Btw, you may want to use a FULL OUTER JOIN instead of only a LEFT OUTER JOIN. Otherwise, you wouldn't get any ID values from the table on the right side of the join if they are only in that table.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #7 (permalink)  
Old 09-02-08, 09:55
mah01 mah01 is offline
Registered User
 
Join Date: Sep 2008
Posts: 5
Quote:
Originally Posted by stolze
Btw, you may want to use a FULL OUTER JOIN instead of only a LEFT OUTER JOIN. Otherwise, you wouldn't get any ID values from the table on the right side of the join if they are only in that table.
Thanks, the ID values will always exist in all rows in both left and right table though.
Reply With Quote
  #8 (permalink)  
Old 09-02-08, 11:03
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
In that case, use a regular INNER JOIN to avoid unnecessary additional processing in DB2.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #9 (permalink)  
Old 09-02-08, 11:05
mah01 mah01 is offline
Registered User
 
Join Date: Sep 2008
Posts: 5
Quote:
Originally Posted by stolze
In that case, use a regular INNER JOIN to avoid unnecessary additional processing in DB2.
Ok, I will. Thank you so much for all your help!

Best regards,
Mattias
Reply With Quote
Reply

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