This question already has an answer here:
Simulate FULL OUTER JOIN with Access on more than two tables
(1 answer)
Closed 8 years ago.
I'm trying to do a full outer join of 3 tables and went off the logic of doing a two-table FOJ from here
So instead of doing a UNION on 3 queries (like I would with two tables), I made 7 queries to do a union of. I'm sure that's really inefficient, but it's my first time using Access.
Basically, I have a table with information about Actual Dollars spent. With specifications on what they were spent on (a number ID), who provided them (a number code), and who received them (a different number code). The other two tables have similar specifications, but one shows what is forecasted to be spent, and the other what was originally budgeted to be spent.
I was able to do a FOJ of two tables and get accurate numbers, but I'm having trouble doing a three-way FOJ.
There are cases where in the Actual Table, for a particular ID, Provider Code, and Receiver Code, there may be 3 records of dollars spent, while in the Forecast table, there may be 2, and in the Budget Table, 1. Essentially there's no guarantee that for each combination of Number ID, provider code, receiver code there are the same number of records in the three tables.
To make sure a record wouldn't multiply I made a query of the 3 tables individually and did the Group By function. And the 7 queries I made were the following:
Table 1: Actual inner join w/ Forecast & A inner join w/ B
Table 2: F inner join w/ B & F left outer join with A (Where A is Null)
Table 3: F inner join w/ A & F left outer join w/ B (Where B is Null)
Table 4: B inner join w/ A & B left outer join w/ F (Where F is Null)
Table 5: A left outer join w/ B (Where B is Null) & A left outer join w/ F (where F is null)
Table 6: B left outer join w/ A (Where A is Null) & B left outer join w/ F (where F is null)
Table 7: F left outer join w/ B (Where B is Null) & F left outer join w/ A (where A is null)
Could someone walk me through how to do a 3-Table full outer join? Is there any combination I'm missing?
Unfortunately I can't post the tables or give too much specification because I'm not allowed to. I also am not able to alter the tables themselves, I basically only have read-only authorization.
You can use union and sequences of left outer joins. I think 3 will do, but you might need 6. Here is a sketch of the SQL:
select *
from A left outer join B left outer join C
union
select *
from B left outer join C left outer join A
union
select *
from C left outer join A left outer join B;
You might need to expand this, depending on the conditions:
select *
from A left outer join B left outer join C
union
select *
from B left outer join C left outer join A
union
select *
from C left outer join A left outer join B
union
select *
from A left outer join C left outer join B
union
select *
from B left outer join A left outer join C
union
select *
from C left outer join B left outer join A;
However, both of these seem like really bad ideas. Instead, just create a temporary table with the ids from all three tables:
select id into ids
from A
union
select id
from b
union
select id
from c;
Then use a left outer join:
select *
from ids left join A left join B left join C;
Or, better yet. Upgrade to a database such as SQL Server Express (also free) that supports more powerful SQL functionality.
Related
In Microsoft SQL Server,
I want to select data that exists in the tables A,C,D and don't exist in B. Can I write like below?
Select A.Store,C.Item,D.Cost
from A
Inner Join C on A.Store=C.Store and A.Item=C.Item
Inner join D on C.Store=D.Store and C.Item=D.Item
And Not exists (Select * from B where A.Store=B.Store and A.Item=B.Item)`
Yes, your query completely makes sense.
You can also use the left join as follows:
Select A.Store,C.Item,D.Cost
from A
Inner Join C on A.Store=C.Store and A.Item=C.Item
Inner join D on C.Store=D.Store and C.Item=D.Item
Left join B on A.Store=B.Store and A.Item=B.Item
Where b.store is null;
How to write an optimized SQL query to get results in table 1 which are not in table 2 and table 3, similarly in table 2 which are not in table 1 and table 3 and for in table 3 which are not in table 1 and table 2
I'm trying to improve performance of my query that I'm Currently working on
TABLE A
LEFT JOIN B
LEFT JOIN C
WHERE B is NULL and C is NULL
UNION
TABLE B
LEFT JOIN A
LEFT JOIN C
WHERE A is NULL and C is NULL
UNION
TABLE C
LEFT JOIN A
LEFT JOIN B
WHERE A is NULL and B is NULL
Is there any way where we can avoid reading the table 3 times?
Try using a full outer join.
select a.*, b.*, c.*
from a full outer join b on
a.A_keys = b.B_keys
full outer join c on
a.A_keys = c.C_keys AND
b.B_keys = c.C_keys
As others have indicated because you did not supply sample data or join condition it is impossible to guess whether the second join (to c) requires conditions to both A and B or just A or just B. So you will need to work that bit out for your self.
For reference, a FULL OUTER JOIN is like doing a LEFT and RIGHT OUTER JOIN in one go. That is, it will return:
all records that match on both sides of the join (the INNER join), plus
all records that are in the LEFT table but not in the right (the LEFT outer join). Right table records will be NULL
all records that are in the RIGHT table but not in the left (the RIGHT outer join). Left table records will be NULL
I will leave it to you to add the "NULL filters" to get the records you are interested in.
So, unique from A, unique from B, unique from C ? As there are no other rows from other tables, I think you just want
SELECT * FROM A
UNION
SELECT * FROM B
UNION
SELECT * FROM C
I have 4 tables , A,B,C,D.
A stores my Customer Id , B ,C D stores various details of customer.
For Eg. B stores Education ,C stores Occupation , D stores Interests.
Now a Customer may have not have entry in C ,D or It may have entry in B and D but not in C.
I want to select all the information by joing all the four tables. Customer_id is present in all the four tables. Inner join will clearly not work.
I am thinking of Left outer join. Could someone help me with the requirement.
An LEFT OUTER JOIN will only get the rows where column A:B match or B:C match, if u want to keep the values of certain rows from table C, D in blank u can first make an
LEFT OUTER JOIN WITH A and B and then make a full outer join with C and D, in that way u are going to be able to keep all the data from C and D no mather if the have or not values in it
I am thinking in somethin like this:
SELECT a.store, b.education, d.storeinterest FROM
(
SELECT * FROM A
LEFT OUTER JOIN B
ON A = B
) A
FULL OUTER JOIN C
ON A = C
FULL OUTER JOIN D
ON A= D
Something along these lines should get you started:
SELECT a.*, b.*, c.*, d.*
FROM a LEFT OUTER JOIN b on
a.customer_id = b.customer_id LEFT OUTER JOIN c
on a.customer_id = c.customer_id LEFT OUTER JOIN d
on a.customer_id = d.customer_id
Can sombody Explains me about joins?
Inner join selects common data based on where condition.
Left outer join selects all data from left irrespective of common but takes common data from right table and vice versa for Right outer.
I know the basics but question stays when it comes to join for than 5, 8, 10 tables.
Suppose I have 10 tables to join. If I have inner join with the first 5 tables and now try to apply a left join with the 6th table, now how the query will work?
I mean to say now the result set of first 5 tables will be taken as left table and the 6th one will be considerded as Right table? Or only Fifth table will be considered as left and 6th as right? Please help me regarding this.
When joining multiple tables the output of each join logically forms a virtual table that goes into the next join.
So in the example in your question the composite result of joining the first 5 tables would be treated as the left hand table.
See Itzik Ben-Gan's Logical Query Processing Poster for more about this.
The virtual tables involved in the joins can be controlled by positioning the ON clause. For example
SELECT *
FROM T1
INNER JOIN T2
ON T2.C = T1.C
INNER JOIN T3
LEFT JOIN T4
ON T4.C = T3.C
ON T3.C = T2.C
is equivalent to (T1 Inner Join T2) Inner Join (T3 Left Join T4)
It's helpful to think of JOIN's in sequence, so the former is correct.
SELECT *
FROM a
INNER JOIN b ON b.a = a.id
INNER JOIN c ON c.b = b.id
LEFT JOIN d ON d.c = c.id
LEFT JOIN e ON e.d = d.id
Would be all the fields from a and b and c where all the ON criteria match, plus the values from d where its criteria match plus all the contents of e where all its criteria match.
I know RIGHT JOIN is perfectly acceptable, but I've found in my experience that it's unnecessary - I almost always just join things from left to right.
> Simple INNER JOIN VIEW code...
CREATE VIEW room_view
AS SELECT a.*,b.*
FROM j4_booking a INNER JOIN j4_scheduling b
on a.room_id = b.room_id;
You can apply join like this..
select a.*,b.*,c.*,d.*,e.*
from [DatabaseName].[Table_a] a
INNER JOIN [DatabaseName].[Table_b] b ON a.id = b.id
INNER JOIN [DatabaseName].[Table_c] c ON b.id=c.id
INNER JOIN [DatabaseName].[Table_d] d on c.id=d.id
INNER JOIN [DatabaseName].[Table_e] e on d.id=e.id where a.con=5 and
b.con=6
Here, at place of a.* and in where condition, you can show column(filed) which you like and according condition in where condition. You can insert more table and database as per your choice. But mind that you need to mention database name and alias if you work in different database.
Just tried the following from the Example DataBase given in W3School. Worked Fine for me.
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate, Products.ProductName, Products.ProductID
FROM Orders
INNER JOIN Products
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;
Join used to combine rows from two or more tables, based on a related column between them. This example from Adventure works:
SELECT a.[EmailAddress],b.[FirstName],b.[LastName],c.[PhoneNumber],d.[Name]
FROM [Person].[EmailAddress] a
INNER JOIN [Person].[Person] b
ON a.BusinessEntityID = b.BusinessEntityID
INNER JOIN [Person].[PersonPhone] c
ON b.BusinessEntityID = c.BusinessEntityID
INNER JOIN [Person].[PhoneNumberType] d
ON c.phoneNumberTypeID = d.phoneNumberTypeID
There are two tables A and B. You are retreiving data from both tables where all rows from B table and only matching rows from A table should be displayed. Which of the following types of joins will you apply between A and B tables?
- Inner join
- Left outer join
- Right outer join
- Self join
Use left outer hoin or right outer join.
For example, the following satisfy your requirement.
select * from tableB
Left outer join tableA
on tableB.ID= tableA.ID
Or
select * from tableA
Right outer join tableB
on tableA.ID= tableB.ID
Better way to understand:
Easy, I would go with (B).
SELECT * FROM B x
LEFT JOIN A y
on x.someColName = y.someColname
EDIT: can also use Right join
SELECT * FROM A x
RIGHT OUTER JOIN B y
on x.someColName = y.someColname
This looks like homework, but it's dead simple enough that I'll just say that you're asking for B LEFT JOIN A.
Join Left
http://www.w3schools.com/Sql/sql_join_left.asp