Impala: duplicate table alias when trying joining on multiple columns - sql

I want to left outer join table A and table B on multiple columns. Below is my code:
select * from table_A
left outer join table_B
on (table_A.a1 = table_B.b1)
left outer join table_B
on (table_A.a2 = table_B.b2)
But then I got error:
HiveServer2Error: AnalysisException: Duplicate table alias: 'table_B'
Does anyone know whatI did wrong here? Thanks!

Use different table aliases as you are joining the same table twice.
select * -- use column names here instead of *
from table_A ta
left outer join table_B tb1 on (ta.a1 = tb1.b1)
left outer join table_B tb2 on (ta.a2 = tb2.b2)

Related

MS Acces Jet SQL Error: Join Expression not supported with multiple Join conditions

I'm trying to run this SQL Expression in Access:
Select *
From ((TableA
Left Join TableB
On TableB.FK = TableA.PK)
Left Join TableC
On TableC.FK = TableB.PK)
Left Join (SELECT a,b,c FROM TableD WHERE b > 1) AS TableD
On (TableD.FK = TableC.PK AND TableA.a = TableD.a)
but it keeps getting error: Join-Expression not supported.
Whats the problem?
Sorry, im just starting with Jet-SQL and in T-SQL its all fine.
Thanks
The issue is that the final outer join condition TableA.a = TableD.a will cause the query to contain ambiguous outer joins, since the records to which TableA is joined to TableD will depend upon the results of the joins between TableA->TableB, TableB->TableC and TableC->TableD.
To avoid this, you'll likely need to structure your query with the joins between tables TableA, TableB & TableC existing within a subquery, the result of which is then outer joined to TableD. This unambiguously defines the order in which the joins are evaluated.
For example:
select * from
(
select TableA.a, TableC.PK from
(
TableA left join TableB on TableA.PK = TableB.FK
)
left join TableC on TableB.PK = TableC.FK
) q1
left join
(
select TableD.a, TableD.b, TableD.c, TableD.FK from TableD
where TableD.b > 1
) q2
on q1.a = q2.a and q1.PK = q2.FK
Consider relating every join to the FROM table to avoid having to nest relations.
SELECT *
FROM ((TableA
LEFT JOIN TableB
ON TableB.FK = TableA.PK)
LEFT JOIN TableC
ON TableC.FK = TableA.PK)
LEFT JOIN
(SELECT FK,a,b,c
FROM TableD WHERE b > 1
) AS TableD
ON (TableD.FK = TableA.PK)
AND (TableD.a = TableA.a)

left join using on clause with error:"both left and right aliases encountered in join" and using where clause but filter null values

I am using code:
create table table3 as
select a.*,b.*
from
table1 a
left join
table2 b
on a.id=b.id
where a.date>=b.date
and a.age<b.age
however,table1 has 20000 rows and table3 has only 5000 rows.It seems the where clause filter all the null values.
When I am using code:
create table table4 as
select a.*,b.*
from
table1 a
left join
table2 b
on (a.id=b.id
and a.date>=b.date
and a.age<b.age)
I would get an error "both left and right aliases encountered in join",because I used inequality in join conditions.
So,how could I get more than 20000 rows include null values by using left join?Should I join multiple times or could use another more effective way?

MSAccess Join SQL query - Join expression not supported

Joining two tables together within vb.net but getting the following error:
"Join expression not supported"
SELECT * FROM (General_Counters_Table AS a INNER JOIN Timers_Table AS b ON b.ulProductionTime = a.Product_ID) INNER JOIN Timers_Table AS b ON b.ulSetupTime = a.Product_ID
Product_ID exists in both General_Counters_Table and Timers_Table
The parser got confused when you join for a second time the Timers_Table because you use the same alias already used for the first join.
However it seems that you just want to produce a result with all fields from the A table and some fields from the B table. If this is the case you need
to join the two tables with the common field (Product_ID) and add, to the SELECT statement, the fields required from the A and B table
SELECT a.*, b.ulProductionTime, b.ulSetupTime, .......
FROM General_Counters_Table AS a
INNER JOIN Timers_Table AS b ON b.Product_ID = a.Product_ID

How to take distinct values in hive join

I need to take the distinct values from Table 2 while joining with Table 1 in Hive. Because the table 2 has duplicate records.
Considering below join condition is it possible to take only distinct key_col from table 2? i dont want to use select distinct * from ...
select * from Table_1 a left join Table_2 b on a.key_col = b.key_col
Note: This is in Hive
Use Left semi join. This will give you all the record in table1 which exist in table2(duplicate record) without duplicates.
select a.* from Table_1 a left semi join Table_2 b on a.key_col = b.key_col

Hive : Checking if a string from table 1 is present in a list of strings from table 2 while joining two tables

I am trying to join on whether a string(a column from table 1) is present in list of strings(a column from table 2) in Hive QL. Can anyone please help me with the syntax.
SELECT
A.id
FROM tab1 A
inner join tab2 B
ON (
(array_contains(B.purchase_items, A.item_id) = true )
)
Above SQL does not work.
First, unless Hive QL is backwards, your query is wrong upfront:
SELECT A.ID FROM A tab1
will return nothing because you've declared table "A" as "tab1". Either reverse the Alias or correct the table alias reference: (I assume tab1 is the table name, so go with option 1)
SELECT A.ID from tab1 A
--OR
SELECT tab1.id from A tab1
Second, joins do not work based on conditional criteria, they ARE the conditional criteria. Sort of...
For example:
SELECT A.ID
FROM tab1 A
INNER JOIN tab2 B
ON A.item_id = B.purchase_item
is almost like doing a simple cross join with a WHERE condition:
SELECT A.ID
FROM tab1 A, tab2 B --better to use it straight as "FROM tab1 A cross join tab2 B"
WHERE a.item_id = b.purchase_item
You can use LEFT SEMI JOIN, which would retrieve rows from left side table with columns matched from right side table.
SELECT A.id FROM tab1 A
LEFT SEMI JOIN tab2 B
ON A.col1 = B.col1 AND <any-other-join-cond>;
Note that the SELECT and WHERE clauses can’t reference columns from the right hand table.