Inner and left join on the same tables - sql

I have two tables (A and B) with two columns in common (x and y). I'd like to inner join A and B on x but keep only the values of A's column y (the left join). I'm looking for a way that will combine the two y columns (can't just specify A.y in the select statement). How can I do this?
Example
Table A
x y
1 2
3 4
5 6
7 8
Table B
x y
1 2
3 8
9 null
11 0
I'd like the resulting table to look like
x y
1 2
3 4

select a.x, a.y
from TableA a
inner join TableB b on a.x = b.x

Do you mean:
SELECT *
FROM A
INNER JOIN B b1 ON A.x = b1.x
LEFT JOIN B b2 ON a.y = b2.y

Take a look at SQL exclude a column using SELECT * [except columnA] FROM tableA? Second answer. Not the best solution, but you can use this as a workaround. In general, you should specify the full list of columns explicitly.

Related

best way to join by multiple columns and avoid OR in SQL

I need a recommendation.
I have two tables. Table 1 is the main table and table 2 is the table that I initially thought to join Table 1 through a left join, table 2 is much larger than table 1. What would be the best performing way to join Table 1 and Table 2 being the union condition that Column b is equal to column b or that column c is equal to column c and column d is equal to column d, that is, any of these conditions is met but no empty values are met. This without using OR in the left join due to the poor performance it would have and the execution time. I appreciate any help.
Note: table 1 and table 2 is the result of 40 lines query. Database do not support recursive query. The database is sap hana.
Table 1
ID
column b
column c
column d
1
d
g
j
2
e
h
k
3
f
i
Table 2
ID_2
column b
column c
column d
4
d
g
5
k
6
i
Desired Result
ID
column b
column c
column d
ID_2
1
d
g
J
4
2
e
h
k
5
3
f
i
6
Use two left joins:
select t1.*,
coalesce(t2_b.id_2, t2_c.id_2, t2_d.id_2) as id_2
from table1 t1 left join
table2 t2_b
on t1.b = t2_b.b left join
table2 t2_c
on t1.c = t2_d.c and t2_b.b is null
table2 t2_d
on t1.d = t2_d.d and t2_c.c is null;
Note that for optimal performance, you want three indexes:
table2(b, id_2)
table2(c, id_2)
table2(d, id_2)

SQL: select only data from one column, for which one of the value from second column is x

I have a problem which I can't describe without explaining this on this example:
So there are 2 columns like:
X Y
A 2
A 1
A 3
B 3
C 2
A 1
D 2
B 1
B 3
C 1
A 1
D 3
D 1
and now I would like to select only that data from X, where one of the values from Y is 2.
So my output should look like:
X Y
A 2
A 1
A 3
C 2
A 1
D 2
C 1
A 1
D 3
D 1
because Y=2 for X=B doesn't exist in the main table.
My question is what is the query for this operation? I tried something with CASE WHEN but something didn't fix for me.
Try
SELECT X FROM Table WHERE X IN (SELECT X FROM Table WHERE Y=2)
OR Try
SELECT t1.X FROM Table t1
INNER JOIN Table t2 ON t1.X = t2.X
WHERE t2.Y = 2
Try a subquery:
SELECT X FROM table WHERE X IN (SELECT X FROM table WHERE Y = 2);

SQL how to translate multiple columns?

Suppose I have a table A as a 'dictionary':
A type code name
1 a x
1 b y
2 a z
2 b t
I want to translate the following table B in to names:
B c1 c2
a a
a b
b b
Where B.c1 is the code of A.type=1 and B.c2 is the code of A.type=2.
The expected result is
x z
x t
y t
If there is only one column in B needs to be translated, it is easy.
SELECT A.name
FROM A, B
WHERE A.type = 1
AND B.c1=A.code
Using joins;
select a1.type,b.c1,a2.type,b.c2 from b
left outer join a as a1 on b.c1= a1.code and a1.type=1
left outer join a as a2 on b.c2= a2.code and a2.type=2

Find column from multiple column

I have one question about database query. Please refer below table.
Table : 1
ID Country
1 x
2 y
3 z
4 k
Table : 2
eng fre fre1 fre2
x x
x1 k y t
x2 n z
Output Table
id country
1 x
2 x1
3 x2
4 x1
How to achieve this in Hive?
Thank you so much for help.
You can join three times but it may run slow:
select a.id, coalesce(b.eng, c.eng, d.eng) as Country
from table_1 a
left join table_2 b on a.country=b.fre
left join table_2 c on a.country=c.fre1
left join table_2 d on a.country=d.fre2
;

Merge two columns into one column and rest of show different from two different tables

I have two tables A & B.
Table A has x and y columns, and B has x and z columns.
Table A
X y
---------
aa 1
bb 2
cc 3
dd 4
Table B
x z
------
aa 5
ee 6
dd 7
ff 8
And I want result is like this:
Result
x y z
----------------
aa 1 5
bb 2
cc 3
dd 4 7
ee 6
ff 8
Can you please help to sort out this problem?
If you are using SQL Server try this:
Select COALESCE(A.x,B.x),Y,Z
FROM TableA A FULL OUTER JOIN TableB B
ON A.X = B.X
See this SQLFiddle
If you are using MySQL try this:
Select COALESCE(A.X,B.X),Y,Z from TableA A
Left join TableB B on A.X = B.X
UNION
Select COALESCE(A.X,B.X),Y,Z from TableA A
RIGHT join TableB B on A.X = B.X
See this SQLFiddle
try this:
select COALESCE(A.x,B.x),y,z
from TableA A full outer join tableB B
on A.X=B.X
SQL Fiddle demo
You probably need to join tables A and B on the key X.
Select A.X, A.Y, B.Z From A full outer join B on A.X = B.X
Read more about joins in SQL: http://en.wikipedia.org/wiki/Join_%28SQL%29.