473,441 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,441 software developers and data experts.

Full Outer Joins

6
I had a similar question.

I was wondering how I should approach a situation where any one of the three tables can be null.

Your suggestion works if the third table is the only table that is null. How do I alter this so that accommodations can be made for the fact that any one of the tables may be blank at any given time?

Thanks!

** Admin Edit **
This thread was created as a result of a hijack in another thread (How to modify an inner join search query to return all fields even if a table is null).

This conversation can now properly be continued in here.

-NeoPa.
Jul 26 '10 #1
14 2072
Ive never had to do this before but I think a UNION ALL would serve the purpose your looking for.
Jul 27 '10 #2
Actually I take that back...Union would require that all the tables have the same amount of columns and DataTypes. After thinking about this I think just doing some LEFT and RIGHT JOINS would work. As they return all columns even if there are no matches.
Jul 27 '10 #3
akhos1
6
@benwizzle
It is my understanding that outer joins return ALL values of one of the tables (i.e. left join returns all values for the left table and shows matching values between tables, even if the right table is null and vice versa for right join).

My issue is that any table can be empty. Either table 1 or table 2 or table 3. The outer join would work fine if I knew which table would be empty, but I don't. I'm trying to avoid outer joins all together.

I would really appreciate the help. I'm stumped
Jul 27 '10 #4
ck9663
2,878 Expert 2GB
It looks like you want to return all rows regardless if it's matched or not. I don't think there's any other way than FULL OUTER JOIN...

Good Luck!!!

~~ CK
Jul 28 '10 #5
akhos1
6
@b0010100
Maybe I'm not being clear. Let's make a random assumption that table B is the blank table. An inner join with tables A and B would yield no results, therefore the union with table AB and table C would show all values in table C.
The code you put up there works perfectly, but I want to find out how to modify it such that any table may be null at any time, but I can still find matched values in the tables that actually are populated.
Jul 28 '10 #6
Jerry Winston
145 Expert 100+
Easy, just throw in conditionals for you table scenarios:
Expand|Select|Wrap|Line Numbers
  1. DECLARE @TableARowCount INT
  2. DECLARE @TableBRowCount INT
  3. DECLARE @TableCRowCount INT
  4. DECLARE @Tables TABLE (tableID INT IDENTITY(1,1),tName  varchar(40))
  5. DECLARE @populatedTables INT
  6. DECLARE @sqlString VARCHAR(4096) 
  7.  
  8. SET @TableARowCount = (SELECT COUNT(*) FROM TableA)
  9. SET @TableBRowCount = (SELECT COUNT(*) FROM TableB)
  10. SET @TableCRowCount = (SELECT COUNT(*) FROM TableC)
  11.  
  12. IF (@TableARowCount > 0)
  13. BEGIN
  14. INSERT INTO @Tables VALUES ('TableA')
  15. END
  16.  
  17. IF(@TableBRowCount > 0)
  18. BEGIN
  19. INSERT INTO @Tables VALUES ('TableB')
  20. END
  21.  
  22. IF(@TableCRowCount > 0)
  23. BEGIN
  24. INSERT INTO @Tables VALUES ('TableC')
  25. END
  26.  
  27. SET @populatedTables = (SELECT COUNT(*) FROM @Tables)
  28. IF @populatedTables = 3
  29.     BEGIN
  30.         SET @sqlString = 'SELECT [cols] FROM ' + (SELECT tName FROM @Tables WHERE tableID = 1) + ' T1 INNER JOIN '+(SELECT tName FROM @Tables WHERE tableID = 2)+' ON T1.jf = T2.jf UNION ALL SELECT [cols] FROM ' + (SELECT tName FROM @Tables WHERE tableID = 3)
  31.         EXEC sp_executesql @sqlString
  32.     END
  33. ELSE 
  34.     BEGIN
  35.         IF @populatedTables = 2
  36.             BEGIN
  37.                 SET @sqlString = 'SELECT [cols] FROM ' + (SELECT tName FROM @Tables WHERE tableID = 1) + ' T1 INNER JOIN '+(SELECT tName FROM @Tables WHERE tableID = 2)+' ON T1.jf = T2.jf '
  38.                 EXEC sp_executesql @sqlString
  39.             END
  40.         ELSE 
  41.             BEGIN
  42.                 IF @populatedTables = 1
  43.                     BEGIN
  44.                         SET @sqlString = 'SELECT [cols] FROM ' + (SELECT tName FROM @Tables WHERE tableID = 1) 
  45.                         EXEC sp_executesql @sqlString
  46.                     END    
  47.             END
  48.     END
  49.  
  50.  
INNER JOINing tables based on whether or not they are null is a requirement I have never faced. That's why I love Bytes.com, you get to solve the most unique problems!
Jul 28 '10 #7
akhos1
6
@b0010100
WOW, this looks great! I'll try it out this afternoon and I'll let you know how it works.

Thanks a million!
Jul 28 '10 #8
ck9663
2,878 Expert 2GB
I did not read b0010100's entire solution. The only other thing I would recommend is on the checking if a table is empty. And this is true to most situation.

If you only need to check if a table is not empty, instead of:

Expand|Select|Wrap|Line Numbers
  1.  
  2. if (select count(*) from tablename) > 0
  3.    select 'table is not empty'
  4. else
  5.    select 'table is empty'
  6.  
  7.  
do this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. if exists (select 1 from tablename) 
  3.    select 'table is not empty'
  4. else
  5.    select 'table is empty'
  6.  
  7.  
The difference? You don't need the count of the entire table. The exists function returns the result set immediately and will not continue to read the entire table. The count(*) check is good for smaller tables and, if possible, should be avoided.

Happy Coding!!!

~~ CK
Jul 28 '10 #9
Jerry Winston
145 Expert 100+
Mmmm, refactoring.... :)
Thanks CK!
Jul 29 '10 #10
NeoPa
32,556 Expert Mod 16PB
If anyone feels their posts belong in the previous thread (I've made a mistake on any) then please let me know and I can move them back across for you.

It all got a bit messy and I wasn't clear exactly who you were all replying to (in some cases at least - not all).
Aug 2 '10 #11
NeoPa
32,556 Expert Mod 16PB
I think CK made the suggestion of FULL OUTER JOINs in post #5. There is much confusion over this so I think it may be worth revisiting that. SQL JOINs describes what's available generally, but mainly from a Jet (MS Access) perspective. The only difference for T-SQL is that the full term is FULL OUTER JOIN rather than the simpler OUTER JOIN as used in the article.

I think, if I understand your question correctly, that the following concept should work for you :
Expand|Select|Wrap|Line Numbers
  1. (([TableA] FULL OUTER JOIN [TableB]) FULL OUTER JOIN [TableC])
I suspect this is much as CK was suggesting when he mentioned it originally. You would need to provide the ON criteria for both joins of course.
Aug 2 '10 #12
akhos1
6
@NeoPa
My apologies, I didn't realize I was hijacking anyone's post.

In any case, thanks a bunch guys! It took me a while to finish debugging, but I just got done today and it works like a charm. Excellent suggestions!
Aug 2 '10 #13
NeoPa
32,556 Expert Mod 16PB
akhos1: My apologies, I didn't realize I was hijacking anyone's post.
Fair dos. I'm sure you'll know for next time :)

I'm interested to hear which of the posts was most helpful though. It seems you have found a solution anyway.
Aug 2 '10 #14
akhos1
6
b0010100 made the suggestions about stepping through and checking whether or not tables were null. I'm not at all familiar with the syntax he was using (I had initially assumed that it was just in pseudocode). I was able to follow his general logic to obtain the visual basic code that I was looking for.
The inner joins worked exactly like I wanted them to and I'm so happy I didn't have to use any outer joins.

I really appreciated the quick responses I received from this website.
Aug 2 '10 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
4
by: Anthony Robinson | last post by:
I was actually just wondering if someone could possibly take a look and tell me what I may be doing wrong in this query? I keep getting ambiguous column errors and have no idea why...? Thanks in...
1
by: Frank via DBMonster.com | last post by:
Hello, My ODBC driver from DB2 connect does not allow outer joins. Is this normal? Does anyone know how to configure it so it allows outer joins? Greetings, Frank
2
by: Cory | last post by:
i have two tables, Event1 and Event2. They both have fields called contactID and eventID in them. Some of the contactID's between the tables are equal and some are not. I want to do a full outer...
4
by: Brian Parker | last post by:
I'm new to ms-sqlserver ( 2000 ) and need to get an OUTER JOIN working on a three table query. Assumptions: -- I have events in the Event table. -- Each event CAN have one Transaction, but it's...
4
by: jefftyzzer | last post by:
Friends: I have a query similar in structure to the following: SELECT T1.IBMREQD FROM SYSIBM.SYSDUMMY1 T1 LEFT JOIN TABLE
5
by: catlover30 | last post by:
HI, I need help with excel VLOOKUP! I am at a new job and really trying to solve this without asking anyone at work, since they seem th expect me to know this. What I am trying to acheive is the...
1
by: mkepick | last post by:
migrated databases from sybase to mssql, migration exported sybase written queries written with the application build in query tool and imported to mssql databases. all db and tables migrated...
7
by: Tania Louie | last post by:
Hey All, I really need some help here! I currently have 3 databases that I am writing a script for to extract data. I need to join these databases together to get one extract, but I have a...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.