SQL how to translate multiple columns? - sql

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

Related

SQL Multiple left Joins: A to B then A to C

If I have 3 tables A, B and C I understand how to use left join to join them like this:
SELECT *
FROM A LEFT JOIN
B
ON A.col = B.col LEFT JOIN
C
on B.col = C.col
So A to B then B to C
How would I do this:
SELECT *
FROM A LEFT JOIN
B
ON A.col = B.col LEFT JOIN
C
on A.col = C.col
So A to B and then A to C
Would really appreciate any help
I don't see anything wrong with what you're trying to accomplish.
A simple example is at: http://sqlfiddle.com/#!9/6dbbe2/2
There are three tables (A, B, and C):
Table A
id name
1 Alice
2 Bob
3 Carol
4 Don
5 Edith
Table B
id id_A pet
1 5 Tex
2 4 Socks
3 2 Rex
4 1 Percy
5 1 Quinlan
Table C
id id_A hobby
1 1 acting
2 2 boxing
3 4 dancing
4 5 eating
Tables B and C relate to Table A through the id_A foreign key.
A query like you have:
SELECT * FROM A LEFT JOIN B
ON A.id = B.id_a LEFT JOIN C
ON A.id = C.id_a;
works just fine.

Sql finding two columns for values

I have 2 tables joined with inner join with a column
Now rows would have
A B
C D
E G
P Q
Z F
This row I need to compare with Master Relation Table
Column1 Column2
A B
D C
E F
So based on the above condition I need to show records in a report
A B Do Not Show
C D Do Not Show
E G Show
P Q Show
Z F Show
You can do it this using LEFT JOIN:
SELECT T1.Col1,T1.Col2,
CASE WHEN T2.Column1 IS NOT NULL THEN 'Do Not Show' ELSE 'Show' END AS Result
FROM Table1 T1 LEFT JOIN
Table2 T2 ON
(T1.Col1=T2.Column1 AND T1.Col2=T2.Column2)
OR (T1.Col1=T2.Column2 AND T1.Col2=T2.Column1)
Result:
Col1 Col2 Result
---------------------------
A B Do Not Show
C D Do Not Show
E G Show
P Q Show
Z F Show
Sample result in SQL Fiddle

How to order a table based on other table in SQL Server

I have one table1
c1 c2
a 1
a 2
a 3
b 1
b 2
b 3
I have one table2
c3 c4
A I
B II
C III
If Cross Join Table1 & Table2 into Table3
c1 c2 c3 c4
a 1 A I
a 2 A I
a 3 A I
b 1 A I
b 2 A I
b 3 A I
a 1 B II
a 2 B II
a 3 B II
b 1 B II
b 2 B II
b 3 B II
a 1 C III
a 2 C III
a 3 C III
b 1 C III
b 2 C III
b 3 C III
If you add one column in Table1 and cross join with Table2 and insert into Table3
the order of Table3 is missing.
So my question is, how to maintain the order based on the Table1?
Don't worry about the order of inserts. You should include whichever field you want to order by in the insert and use order by when selecting the data.
Try this -
select *
from table3
order by c1, c2
That's as good as your going to get with your current schema.

Inner and left join on the same tables

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.

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.