Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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...
Reply With Quote
  #2 (permalink)  
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/
Reply With Quote
  #3 (permalink)  
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
Reply With Quote
  #4 (permalink)  
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...


Reply With Quote
  #5 (permalink)  
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
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 Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 02:28 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.