Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 24th, 2003, 03:00 PM
Authorized User
 
Join Date: Oct 2003
Location: , , .
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default join

I have the following tables:

Table1:
ORG
    A
    B
    C
    D
-------------------
Table2:
ORG Num1
    A 5
--------------------
Table3:
ORG Num2
    A 2
    D 3
--------------------
Table4:
ORG Num3
    B 1
    D 7
--------------------
Table5:
ORG Num4
    B 4
    C 3

I would like to join all the records without any duplicate data.

I would like to create the following table:
ORG Num1 Num2 Num3 Num4
    A 5 2 0 0
    B 0 0 1 4
    C 0 0 0 3
    D 0 3 7 0

Thanks for your help...
 
Old October 24th, 2003, 03:29 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Well, you'll need to use left joins because you're connecting tables where the org value doesn't exist in ALL tables, so the join must create NULL for the num columns for the tables in which a corresponding ORG value doesn't exist.

Code:
SELECT table1.ORG, table2.num1, table3.num2, table4.num3, table5.num4
FROM table1
LEFT JOIN table2 ON table1.org = table2.org
LEFT JOIN table3 ON table1.org = table3.org
LEFT JOIN table4 ON table1.org = table4.org
LEFT JOIN table5 ON table1.org = table5.org
Should do it, though you'll get NULL values, not zeros, for the numbers where the ORG value doesn't exist in the table.


Take care,

Nik
http://www.bigaction.org/
 
Old October 25th, 2003, 08:18 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

You could wrap each result column in a COALESCE statement to translate the NULL values to a 0:
Code:
SELECT table1.ORG, COALESCE(table2.num1,0) as NUM1,
    COALESCE(table3.num2,0) as NUM2, COALESCE(table4.num3,0) as NUM3,
     COALESCE(table5.num4,0) as NUM4)
FROM ...
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old October 27th, 2003, 01:37 PM
Authorized User
 
Join Date: Oct 2003
Location: , , .
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you all. I did not mentioned that I have been working on access not mysql. I used IIF(ISNULL.... ) function instead of COALESCE.
My program is working fine now.

Thanks again...


 
Old October 27th, 2003, 03:37 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

I think the nz function will do what you want, a bit simpler - e.g.
Code:
SELECT SELECT table1.ORG, nz(table2.num1,0) as NUM1,
...
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com




Similar Threads
Thread Thread Starter Forum Replies Last Post
join dhara_adh SQL Server 2000 2 January 2nd, 2007 12:39 AM
Join or not join madhukp SQL Server 2000 3 February 2nd, 2005 01:11 AM
More than one join jaywhy13 Classic ASP Basics 2 January 31st, 2005 11:38 PM
INNER JOIN msmagied Classic ASP Databases 3 August 29th, 2004 12:28 PM
Oracle 8i inner join and left join problem puteri_84 Oracle 2 August 19th, 2004 07:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.