Join multiple Tables based on multiple criteria over one field - sql

I need find the most efficient way to join one table, to other three, using as criteria the values on theirs [Id_Orig] fields
Consider Table1 as the one with our universe of data, having the fields Below:
Select Id_Orig, F1, F2 From Table1
The field [Id_Orig] can have only three values: 'DO', 'CC' and 'DP'. I need to join other three tables with Table1, based on those values as shown below:
Table1 left join Table_DO : only for those records that have both [Id_Orig] = 'DO'
Table1 left join Table_CC : only for those records that have both [Id_Orig] = 'CC'
Table1 left join Table_DP : only for those records that have both [Id_Orig] = 'DP'
Suppose that Table1 has 1000 records, these must remain unchanged. The idea is only to add the fields from the other respective linked tables, as shown below:
Table1.Id_Orig, Table1.F1, Table1.F2, Table_DO.*, Table_CC.*, Table_DP.*
Can anyone tell me, please, how is the best way to achieve that, and if that could be done on the 'ON' Clause after the Left Join?
Thanks in advance.
Leopoldo Fernandes
Portugal

Try something like:
SELECT t1.*, tdo.*, tcc.*, tdp*
FROM Table1 AS t1
LEFT OUTER JOIN Table_DO AS tdo ON t1.[Id_Orig] = tdo.[Id_Orig] AND t1.[Id_Orig] = 'DO'
LEFT OUTER JOIN Table_CC AS tcc ON t1.[Id_Orig] = tcc.[Id_Orig] AND t1.[Id_Orig] = 'CC'
LEFT OUTER JOIN Table_DP AS tdp ON t1.[Id_Orig] = tdp.[Id_Orig] AND t1.[Id_Orig] = 'DP'
For example, when you JOIN to Table_DO, you said you want when both tables have [Id_Orig] = 'DO' ... since the JOIN condition is the values are equal, you only need to specify one column (I choose Table1).

Related

What is the simplest way to join multiple tables in SQL?

I have been working as a data analyst for about 4 months now and the above is a very real question for me. The most recent way I've been taught to join is with the left join with the following example.
left join table1
on
table2.id = table1.id
left join table2
on
table3.table_id = table2.table_id
left join table4
on
table1.tablekey_id = table4.tablekey_id
Looking for the most efficient way to connect multiple tables to save time, if possible.
Thanks in Advance!
You could certainly use a shorter alias for the table names and table fields.
Example:
Table 1 = alias t1
tablekey_id = tkid
So then it would t1.tkid instead of having to type out table1.tablekey_id.
If it is only two tables of same columns and same datatypes, you can use union concept to join the tables.
You can also join tables like this:
Tables: TableA, TableB
SELECT
column1,
column2,
column3
FROM TableA, TableB
WHERE TableA.id = TableB.id;

Vlookup equilivant in presto sql

Is there a way of doing something like a vlookup in presto?
I am given a bunch of IDs in one table that looks like this.
I have another table that gives me back the lookup values. so 503203 would have a particular name, a price, and a category.
How can I add three columns after each named product to show me the corresponding values? some thing like this?
Use left join:
select t1.*,
t21.*, t22.*, t23.*, t24.*
from table1 t1 left join
table1 t21
on t1.product1 = t21.productid left join
table1 t22
on t1.product2 = t22.productid left join
table1 t23
on t1.product1 = t23.productid left join
table1 t24
on t1.product1 = t24.productid ;

SQL Different between Left join on... and Left Join on..where

I have two sql to join two table together:
select top 100 a.XXX
,a.YYY
,a.ZZZ
,b.GGG
,b.JJJ
from table_01 a
left join table_02 b
on a.XXX = b.GGG
and b.JJJ = "abc"
and a.YYY between '01/08/2009 13:18:00' and '12/08/2009 13:18:00'
select top 100 a.XXX
,a.YYY
,a.ZZZ
,b.GGG
,b.JJJ
from table_01 a
left join table_02 b
on a.XXX = b.GGG
where b.JJJ = "abc"
and a.YYY between '01/08/2009 13:18:00' and '12/08/2009 13:18:00'
The outcome of them is different but I don't understand the reason why.
I would be grateful if I can get some help here.
Whenever you are using LEFT JOIN, all the conditions about the content of the right table should be in the ON clause, otherwise you are effectively converting your LEFT JOIN to an INNER JOIN.
The reason for that is that when a LEFT JOIN is used, all the rows from the left table will be returned. If they are matched by the right table, the values of the matching row(s) will be returned as well, but if they are not matched with any row on the right table, then the right table will return a row of null values.
Since you can't compare anything to NULL (not even another NULL) (Read this answer to find out why), you are basically telling your database to return all rows that are matched in both tables.
However, when the condition is in the ON clause, Your database knows to treat it as a part of the join condition.

Optimising SQL query

I have a SQL query that performs an INNER JOIN on two tables having >50M rows each. I wish to reduce the time it takes to search through the join by reducing the rows that are joined based on a column present on one of the tables.
Say I have table1 with columns A,B,C and table2 with columns A,D,E. I wish to join based on column A but only those rows that have value 'e' for column E of table 2.
My SQL query :
SELECT one.B, two.D
FROM table1 one
INNER JOIN table2 two WHERE two.E IN ('e')
ON one.A = two.A
WHERE one.B > 10
AND two.D IN ('...')
It gives the error :
ORA-00905: missing keyword
Where am I going wrong? How do I achieve the intended result?
SELECT one.B, two.D
FROM table1 one
INNER JOIN table2 two -- WHERE two.E IN ('e') --> shouldn't use where here
ON one.A = two.A and two.E = 'e'
WHERE one.B > 10
AND two.D IN ('...')
Comments included in the code.
As vkp pointed out, the WHERE is improperly used. Instead you could also make a subquery to include that where statement. So that:
INNER JOIN table2 two WHERE two.E IN ('e')
becomes
INNER JOIN (select * from table2 WHERE E IN ('e')) two
You could also put the condition in the Where clause
SELECT one.B, two.D
FROM table1 a
JOIN table2 b
ON b.A = a.A
WHERE a.B > 10
And b.E = 'e'
AND b.D In ('...')
EDITED to remove 2nd incorrect suggestion

MS Access Inner Join On 3 Tables with the same field_name

I'm doing an assignment for class, and I'm at my whits end. Basically, I need to write a query that uses INNER JOIN's to combine all the data in 4 tables, while avoiding having titles columns with identical names (Ie Table1.Master_Number and Table2.Master_Number). The literal instructions are as follows:
Step 1: Create a view of all the columns (only list the columns once if the columns are duplicated in the tables) in all the tables of the Lecture 5 database. Save this query as Lecture_5_View and us this query to build the following 3 queries. In the “FROM” clause, you MUST use an “INNER JOIN” in this query. Remember ACCESS does not support the “CREATE VIEW” command so you must create a select query. `
Here is a screenshot of the database, that shows all the headings and column headings.
Based on other posts like this, I thought that it would be pretty simple. I have this so far, but it does not want to run
(ie Syntax error(missing operator) in query expression 'Table1.Master_Number = Table2.Master_Number INNER JOIN Table4 ON Table1.Master_Number = Table4.Master_Number)
SELECT *
FROM Table1
INNER JOIN Table2 ON Table1.Master_Number = Table2.Master_Number
INNER JOIN Table4 ON Table1.Master_Number = Table4.Master_Number
Where am I going wrong so far and how to I accomplish this query?
Much thanks,
Josh
With Access you need to use parentheses when doing more than one join:
SELECT *
FROM (Table1
INNER JOIN Table2
ON Table1.Master_Number = Table2.Master_Number)
INNER JOIN Table4
ON Table1.Master_Number = Table4.Master_Number;
This is essentiall spliting down you query so you have at most one join per section, i.e. Your First query is:
SELECT *
FROM Table1
INNER JOIN Table2
ON Table1.Master_Number = Table2.Master_Number;
Then you have:
SELECT *
FROM YourFirstQuery
INNER JOIN Table4
ON Table1.Master_Number = Table4.Master_Number;
This differs slightly from subqueries as you are still able to reference all fields from Table1 and Table2.
EDIT
To avoid duplicating columns you will need to explicitly list the columns you want (although you should be doing this anyway):
SELECT Table1.Master_Number,
Table1.Asset_Tag,
Table1.Serial_Number,
Table2.Last_Name,
Table2.First_Name,
Table4.Office_Number,
Table4.Location,
Table4.Department
FROM (Table1
INNER JOIN Table2
ON Table1.Master_Number = Table2.Master_Number)
INNER JOIN Table4
ON Table1.Master_Number = Table4.Master_Number;
Try this:
SELECT * FROM ((Table1
INNER JOIN Table2
ON Table1.Master_Number = Table2.Master_Number)
INNER JOIN Table4
ON Table1.Master_Number = Table4.Master_Number)