SQL joining/unioning help - link table b and c to table a? - sql

I have 3 tables in my database. Each of them has one column, "index" that links the fields across all 3.
Our starting point is table a, and the indexes inside it. If the index is not there, I don't need it.
Tables b and c are very similar, and every index listed in table a will be in b or c, or both. All I need to do is make sure that all the fields in table a are joined to fields in table b or c.
I started with:
SELECT *
FROM `table_a`
JOIN table_b ON table_a.index = table_b.index
Which works great. But it will exclude all the indexes in table a which don't match, which is why I believe, when I add:
UNION
FROM `table_a`
JOIN table_c ON table_a.index = table_c.index
I actually get LESS results, rather than more.
Can someone tell me how to say "if the index isn't in table b, then look in table c?"

I'm not sure this is what you're after, but it will give you all the results of a, and any possible matches from b OR c.
SELECT *
FROM table_a
LEFT OUTER JOIN table_b ON table_a.index = table_b.index
LEFT OUTER JOIN table_c ON table_a.index = table_c.index

Did you try a "Union All" ?
SELECT *
FROM `table_a`
JOIN table_b ON table_a.index = table_b.index
UNION ALL
SELECT *
FROM `table_a`
JOIN table_c ON table_a.index = table_c.index

First step:
SELECT * FROM
table_a
LEFT JOIN table_b ON table_a.index = table_b.index
LEFT JOIN table_c ON table_b.index = table_c.index
This will get you all indexes from all 3 tables.
If you want to have the first index which isn't NULL from the tables _a, _b, or _c you can do this like this in MySQL:
SELECT COALESCE(table_a.index, table_b.index, table_c.index) AS firstIndexFromABC
FROM
table_a
LEFT JOIN table_b ON table_a.index = table_b.index
LEFT JOIN table_c ON table_b.index = table_c.index
Or what DB are you using? Update: MySQL
Update after some comments:
Sorry, I still don't get it. That's what the COALESCE method does. You get in 1 column combined the value of a if it's there, if not you get b if it's there, if not you get c.
If you mean, that you want the information, from which table you took the index then try this:
SELECT COALESCE(table_a.index, table_b.index, table_c.index) AS firstIndexFromABC,
CASE WHEN table_a.index IS NULL AND table_b.index IS NULL THEN 'c'
WHEN table_a.index IS NULL AND table_b.index IS NOT NULL THEN 'b'
WHEN table_a.index IS NOT NULL THEN 'a'
END AS whichTable
FROM
table_a
LEFT JOIN table_b ON table_a.index = table_b.index
LEFT JOIN table_c ON table_b.index = table_c.index

Related

Sparksql to select certain records against 3 tables

I have 3 tables and need to fetch the records as below
Table_A,
Table_B,
Table_C
Select only Table_A records which are common in Table_B & Table_C and ignore which are not common in both Table_B & Table_C finally results would be no duplicates.
Approach 1 Tried: inner join Table_A with Table_B and again separate inner join Table_A with Table_C finally did union.
Ab = Table_A.join(Table_B,Table_A["id"] == Table_B["id"], "inner").select(common columns)
Ac = Table_A.join(Table_C,Table_A["id"] == Table_C["id"], "inner").select(common columns)
result = Ab.union(Ac) <<Got more duplicates>>
result = result,dropDuplicates(["id"])
But still I got the duplicates.
Approach 2 Tried with SparkSql:
Table_A
left outer
Table_B
on A.id = B.id
left outer Table_C
on A.id = c.id
In this Approach, no duplicates but more records than Table_A also the uncommon records.
Any suggestion and best approach would be apprciated
In Spark SQL, I would recommend exists:
select a.*
from table_a a
where exists (select 1 from table_b b on b.id = a.id)
and exists (select 1 from table_c c on c.id = a.id)
This does the filtering you want, and will not duplicate records of table_a in the resuletset, even if there are multiple matches in table_b or table_c.

Oracle SQL: Get all users in one table but not another and join to a third table

I am wondering how to use oracle sql to get all the rows that are in one table but not another. The issue I am having is that the two tables don't have a field in common so I need to join to a third master table.
This is what I've tried which doesn't produce any errors but also produces 0 records which isn't possible but clearly I've done something wrong.
SELECT a.USER_ID, c.AD_ID, c.CREATED_DATE_ FROM $A$ a, $C$ c, $B$ b
WHERE (b.USER_ID IS NULL AND a.CUSTOMER_ID = c.CUSTOMER_ID)
I have three tables:
Table A has fields CUSTOMER_ID & USER_ID
Table B has field USER_ID
Table C has field CUSTOMER_ID
I need all the users that are in table C but not table B. They are all in Table A because that is the master list of users.
Any insight would be greatly appreciated.
SELECT
*
FROM
table_a
WHERE
NOT EXISTS (SELECT * FROM table_b WHERE table_b.user_id = table_a.user_id )
AND EXISTS (SELECT * FROM table_c WHERE table_c.customer_id = table_a.customer_id)
My solution:
select * from TableC tc
join TableA ta on tc.CUSTOMER_ID=ta.CUSTOMER_ID
left join TableB tb on tb.USER_ID=ta.USER_ID
where ta.USER_ID is null
I think you want:
select a.USER_ID, c.AD_ID, c.CREATED_DATE_
from a join
c
on a.customer_id = c.customer_id
where not exists (select 1 from b where b.user_id = a.user_id);

SQL - not sure how to join tables

I'm trying to join two tables like this:
Table A
ID Value1
1 A
2 B
3 C
Table B
ID Value2
1 A
3 B
4 C
Result should be:
ID Value1 Value2
1 A A
2 B null
3 C B
4 null C
I.e. join Table A to Table B on ID. If ID doesn't exist in Table A, add the ID from Table B.
The closest I've come is:
SELECT
a.ID, a.Value1, b.Value2
FROM
TableA a
OUTER JOIN
TableB b ON a.ID = b.ID
That gives me the new rows from TableB, but the ID is null.
How can I accomplish this?
You are very close, you just need a little push in the right direction:
SELECT COALESCE(a.ID, B.ID) As ID, a.Value1, b.Value2
FROM TableA a
FULL OUTER JOIN TableB b ON a.ID=b.ID
The COALESCE function returns the first parameter it gets that is not null. since this is a full outer join, a.id will be null on one row and b.id would be null on a different row.
Try this:
SELECT *
FROM TableA A
FULL OUTER JOIN TableB B
ON A.ID = B.ID;
Just a note: you should not name your tables in SQL with spaces in them.
Remember the basic for joining different tables
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;
For your case:
SELECT a.value1, b.value2
FROM TableA a
FULL OUTER JOIN TableB b ON a.ID=b.ID
remember full outer join
The FULL OUTER JOIN keyword returns all rows from the table (tableA) and from the table (tableB) and the FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT joins.

Select from two different tables by value in third table

I have next tables.
First one is A.
A have two columns: A_ID and A_VALUE.
Second table is B. B too have two columns: B_ID and B_VALUE
In additional I have table C. Table C have C_ID and bool columns C_BOOL
If C_BOOL value == true i need select value from A with given ID.
If C_BOOL value == false i need select value from B.
How I can write SELECT for this?
I use oracle db.
Thanks in advice.
SELECT CASE C.BOOL WHEN 1 THEN A.ID ELSE B.ID END
FROM A
JOIN B
ON B.ID = A.ID
JOIN C
ON C.ID = A.ID
Try this query:
SELECT C_ID,CASE WHEN C_BOOL = 1 THEN T3.A_VALUE ELSE T2.B_VALUE END
FROM TABLE_C T1 LEFT OUTER JOIN TABLE_B T2 ON T1.C_ID = T2.B_ID
LEFT OUTER JOIN TABLE_A T3 T2 ON T1.C_ID = T3.A_ID
select decode(C.BOOL,1,A.ID,B.ID) FROM C
JOIN A
ON A.ID=C.ID
JOIN B
ON B.ID=C.ID;
I consider T McKeown answer as valid this is just equivalent (but more compact) I suppose.

Join two tables with one table

I need to do the following (I am not really sure if it is possible to do using SQL)
Table A
col_a_1, col_a_2, col_a_3
Table B
col_a_1, col_b_1, col_b_2....
Table C
col_a_1, col_c_1, col_c_2....
were col_a_1 is unique in table A. I need to join tables A, B, C such that every entry in table A will appear multiple times but every entry in table B and C appears only once and against the same col_a_1 of table A.
I know how to do it using the code. Is it also possible to do with SQL alone?
Thanks in advance.
You either want to use an outer join of some sort. either left or full outer.
SELECT A.*, B.*, C.*
FROM Table_A A
LEFT JOIN Table_B B on A.col_A_1 = B.Col_A_1
LEFT JOIN table_C C on A.Col_A_1 = C.Col_A_1
SELECT A.*, B.*, C.*
FROM Table_A A
FULL OUTER JOIN Table_B B on A.col_A_1 = B.Col_A_1
FULL OUTER JOIN table_C C on A.Col_A_1 = C.Col_A_1
or possibly a union result... just depends on what you're after.
SELECT A.*, B.*
FROM Table_A A
INNER JOIN Table_B B on A.col_A_1 = B.Col_A_1
UNION
SELECT A.*, C.*
FROM Table_A A
INNER JOIN Table_C C on A.col_A_1 = C.Col_A_1