What join columns should I take for ACDOCA and JVSO1 SAP tables? - azure-synapse

I have fetched two SAP ERP tables into Azure Synapse: ACDOCA and JVSO1.
Now I need to join these two tables. And the column EBELN is required to be in join condition, also both tables have around 40% of EBELN empty. Because of these empty values, these two tables produce a lot of data (In Billions).
What I have tried: I have picked one more column EBELP and joined both tables based on these two columns:
WHERE ACDOCA.EBELN = JVSO1.EBELN AND ACDOCA.EBELP = JVSO1.EBELP
But even after this condition, I am getting a lot of data.
What I want:
I want to join these two tables and have less amount of data (Not in Billions). Can you please suggest me more columns in both tables so that I can join both of the tables correctly with lesser amount of data.
Thanks

Joining conditions
on acdoca.ebeln=JVSO1.ebeln and acdoca.ebelp=JVSO1.ebelp and on acdoca.ebeln=JVSO1.ebeln will give same result for rows with null values. I repro'd this with sample data.
Input data with null values:
ebeln
EBELP
null
A
AA
AA
null
B
Joining tables based on ebeln fields:
select * from acdoca full outer join JVSO1 on
acdoca.ebeln=JVSO1.ebeln
Joining tables based on ebeln and ebelp fields:
select * from acdoca full outer join JVSO1 on
acdoca.ebeln=JVSO1.ebeln and acdoca.ebelp=JVSO1.ebelp
Query should be written in such a way whenever ebeln field is null, condition should be on matching ebelp fields JOIN of tables should happen.
select * from acdoca full outer join JVSO1
on
(acdoca.ebeln is not null and acdoca.ebeln=JVSO1.ebeln)
or
(acdoca.ebeln is null and acdoca.ebelp=JVSO1.ebelp)
or
select * from acdoca full outer join JVSO1 on
(isnull(acdoca.ebeln,acdoca.ebelp) =
isnull(JVSO1.ebeln,JVSO1.ebelp))
Even when you add other matching columns, joining conditions should be like above queries.

Related

What's the difference between Join and UNION in SQL

I'm just wondering what's the difference between Join and Union in SQL and in Join, what's the difference between Join and Cross Join? THANKS!
Join: Joins the table based on certain conditions. Lets say Table A has 2 rows rowA1 and rowA2. And you join this with Table B which has 3 rows rowB1 rowB2 rowB3. So the result will be:
rowA1.data RowB1.data
rowA1.data RowB2.data
rowA1.data RowB3.data
rowA2.data RowB1.data
rowA2.data RowB2.data
rowA2.data RowB3.data
But in a union, the result will be:
rowA1.data
rowA2.data
rowB1.data
rowB2.data
rowB3.data
Union will also check for duplicate. The data types should be consistent. The data types of columns returned should be in same order and number as the datatype and no. of columns returned by second table.
Join is a concept. It can be various types liek Inner Join, Outer Join, Cross Join. Cross Join means, there is a missing condition that would uniquely join the table data.

How does JOIN work exactly in SQL

I know that joins work by combining two or more tables by their attributes, so if you have two tables that both have three columns and both have column INDEX, if you use table1 JOIN table2 you will get a new table with 5 columns, but what if you do not have a column that is shared by both table1 and table2? Can you still use JOIN or do you have to use TIMES?
Join is not a method for combining tables. It is a method to select records (and selected fields) from 2 or more tables where every table in the query must carry a field that can be matched to a field in another table in the query. The matched fields need not have the same name, but must carry the same type of data. Lacking this would be like trying to create meaning from joining a list of license plates of cars in NYC, with height data from lumberjacks in Washington state -- not meaningful.
Ex:)
Select h.name, h.home_address, h.home_phone, w.work_address,
w.department
from home h, work w
where h.employee_id = w.emp_id
As long as both columns: employee_id and emp_id carry the same information this query will work
In Microsoft Access, to get five rows from a three column table joined to a two column table, you'd use:
SELECT Table1.*, Table2.* FROM Table1 INNER JOIN Table2 ON Table1.Field1 = Table2.Field1;
You can query whatever you want, and join whatever you want, though.
If your one table is a list of people, and your other is a list of cars, and you want to see what people have names that are also models of cars, you can do:
SELECT Table1.Name, Table1.Age, Table2.Make, Table2.Year
FROM Table1 INNER JOIN Table2 ON Table1.Name = Table2.Model;
Only when Name is the same as Model will it show a record.
This is the same idea for joining tables in any relational DBMS I've used.
You are right you can join two tables even if they do not have shared column.
Join uses primary to prevent mistakes on inserting or deleting when user trying to insert record that does not has a parent one or some thing like this.
join methods has many types you can view them here:
http://dev.mysql.com/doc/refman/5.7/en/join.html
LEFT JOIN: select all records from first table, then selecting all records from second table that fulfilling the condition after ON clause.
you can't join the tables if they do not share a common column. If you can find a 3rd table that has common columns with table1 and table2 you can get them to join that way. so join table2 and tabl3 on a common column and than join table3 back to table1 on a common column.

SQL Select - Fetching two different values from another table based on two different IDs

I have two tables
Table 1 has five columns
EmployeeID,
EmployeeCarModelID,
EmployeeCarModelName,
SpouseCarModelID,
SpouseCarModelName
Table 2 has two columns
CarModelID,
CarModelName
How would I construct a select statement which will pull through the CarModelName to both the EmployeeCarModelName and the SpouseCarModelName based on their respective IDs? I'm not sure I can use a JOIN statement to do this as we are looking at two different id columns within the same table.
You need two joins to do this. I think you want:
select t1.EmployeeId, t1.EmployeeCarModelID, t2emp.CarModelName as EmployeeCarModelName,
t1.SpouseCarModelID, t2sp.CarModelName as SpouseCarModelName
from table1 t1 left join
table2 t2emp
on t1.EmployeeCarModelID = t2emp.CarModelId left join
table2 t2sp
on t1.SpouseCarModelId = t2sp.CarModelId;

When joining 2 tables one table comes up null

I am joining 2 tables on the first table I get all the relevant data on the second table I only get nulls. There are no nulls in either table Can any one tell me why this is happening?
select * from apmast
left join apitem
on apmast.fvendno + apmast.fccompany = apitem.fcinvkey
There is a problem with your ON that's resulting in you not getting matching records. A LEFT JOIN means that you should get all data from the left table and only the matching records from the right table, or else NULL where there are no matching records. The key to the join, however, is the ON statement. Make sure that apmast.fvendno + apmast.fccompany is actually equal to apitem.fcinvkey.
here is a explanation on the types of joins just incase you get stuck in the future.
INNER JOIN this will get only the rows that match in both the FROM clause and the JOINING table.
LEFT OUTER JOIN this gets all the rows from the table specified in the FROM clause and only the rows that match in the JOINING table.
RIGHT OUTER JOIN this gets all the rows from the table specified in the JOIN clause and only the rows that match in the FROM clause.
FULL OUTER JOIN this will get all the rows from both tables.
SELF JOIN this is used when you need to join the table back to its self to return data.

Inner join between two tables with same count values

I have been working on this issue since 2 days now.
I have two tables created by using SQL Select statements
SELECT (
) Target
INNER JOIN
SELECT (
) Source
ON Join condition 1
AND Join condition 2
AND Join condition 3
AND Join condition 4
AND Join condition 5
The target table has count value of 10,000 records.
The source table has count value of 10,000 records.
but when I do an inner join between the two tables on the 5 join conditions
I get 9573 records.
I am basically trying to find a one to one match between source and target table. I feel every field from target matches every field in source.
Questions:
Why does my inner join give less records even if there are same value of records in both tables?
If it is expected, how can I make sure I get the exact 10,000 records after the join condition?
1) An INNER JOIN only outputs the rows from the JOINING of two tables where their joining columns match. So in your case, Join Condition1 may not exist in rows in both tables and therefore some rows are filtering out.
2) As the other poster mentioned a left join is one way. You need to look which table source or target you want to use as your master i.e. start from and return all those rows. You then left join the remaining table based on your conditions to add all the columns where you join conditions match.
It's probably better if you give us the tables you are working on and the query\results you are trying to achieve.
There's some really good articles about the different joins out there. But it looks like you'd be interested in left joins. So if it exists in Target, but not in Source, it will not drop the record.
So, it would be:
SELECT(...) Target
LEFT OUTER JOIN
SELECT(...) Source
ON cond1 and cond2 and cond3 and cond4 and cond5
Give that a shot and let me know how it goes!
Sometime you need to rely on logical analysis rather than feelings. Use this query to find the fields that do not match and then work out your next steps
SELECT
Target.Col1,Source.Col1,
Target.Col2,Source.Col2,
Target.Col3,Source.Col3
FROM
(
) Target
FULL OUTER JOIN
(
) Source
ON Target.Col1=Source.Col1
AND Target.Col2=Source.Col2
AND Target.Col3=Source.Col3
WHERE (
Target.Col1 IS NULL
OR Source.Col1 IS NULL
OR Target.Col2 IS NULL
OR Source.Col2 IS NULL
OR Target.Col3 IS NULL
OR Source.Col3 IS NULL
)