I have two tables: table1 and table2. I can join them using id1 or id2. I prefer to use id1, but as in some rows id1 is missing, so I have to use id2. Is the following syntax correct:
SELECT *
FROM table1 as a
LEFT JOIN table2 as b
ON (a.id1 is not null and a.id1 = b.id1) or
(a.id2 is not null and a.id2 = b.id2)
It returns some results but I want to be sure if it is valid as I haven't seen it used before.
Are there better ways to do this?
Looks like you have a decent answer in the comments, but to toss another possibility into the ring, you could run both queries and union them.
select *
from table1 as a
LEFT JOIN table2 as b
on a.id1 = b.id1
union
select *
from table1 as a
LEFT JOIN table2 as b
on a.id2 = b.id2
The union will eliminate any duplicates between the sets, and will return records where either condition is true, much like your or statement. Performance wise, the union is probably a little slower, but gives you easier control over the sets. For instance if you only want set 2 to return results when id1 is null, just add it to the where clause. Anyway hope that helps.
Related
I run two SQL queries: The first one have an outer reference to the table inside subquery. In the second one I add the same table inside subquery. The results are different, it fails due to multiple rows.
The first one runs on Oracle, but fails on Spark-SQL. Therefore I am looking for a solution similar to Oracle SQl as in the first SQL code.
Query 1:
select *,
(select N_CODE
from table2 f
where f.ID1 = (select min(f.ID1)
from table1 a left join table2 f on a.ID2 = f.ID2
where a.ID2 = table1.ID2
)
) AS CODE
from table1
Query 2:
select *,
(select N_CODE
from table1 t, table2 f
where f.ID1 = (select min(f.ID1)
from table1 a left join table2 f on a.ID2 = f.ID2
where a.ID2 = t.ID2
)
) AS CODE
from table1
The second one is my solution to the first one in Spark SQL, but it fails on both Oracle and Spark. How can I run the first query on Spark SQL similar to Oracle?
Please do not modify the structure of the query.
Oracle supports multiple inner queries but spark does not. The best way to overcome it is to split your super query into pieces and use join them.
For instance run this part and save it as a table3:
select min(table2 .ID1)
from table1 a left join table2 f on a.ID2 = f.ID2
where a.ID2 = t.ID2
from table2
Then use it for your main query:
....
where f.ID1 = table3
Given this SQL query:
select A.field1 as field
from A left join B
on A.field1 = B.field1
union all
select A.field2 as field
from A left join B
on A.field2 = B.field1;
Is there some way to get the same with few lines? i.e: is there another way to get two columns from a table A joined each one separated with a column from the table B, and then both put into the same resultset column?
The reason for needing that is the real query has a more complex join and where conditions, and for that is a bit big and, almost all, redundant.
Thank you in advance!
With the left join and assuming that the join to B doesn't produce duplicates, then your query is more simply written as:
select A.field1 as field from A
union all
select A.field2 from A;
The left join to B doesn't do anything in this case.
I assume your query is more complicated or you intend an inner join. You could do the union all before the join:
select A.field
from (select field1 from A union all
select field2 from A
) a join
B
on A.field = B.field1 ;
Whether or not this has worse or the same performance depends on the nature of the data.
I'm working in Access 2007 and know nothing about SQL and very, very little VBA. I am trying to do a union query to join two tables, and delete the duplicates.
BUT, a lot of my duplicates have info in one entry that's not in the other. It's not a 100% exact duplicate.
Example,
Row 1: A, B, BLANK
Row 2: A, BLANK, C
I want it to MERGE both of these to end up as one row of A, B, C.
I found a similar question on here but I don't understand the answer at all. Any help would be greatly appreciated.
I would suggest a query like this:
select
coalesce(t1.a, t2.a) as a,
coalesce(t1.b, t2.b) as b,
coalesce(t1.c, t2.c) as c
from
table1 t1
inner join table2 t2 on t1.key = t2.key
Here, I have used the keyword coalesce. This will take the first non null value in a list of values. Also note that I have used key to indicate the column that is the same between the two rows. From your example it looks like A but I cannot be sure.
If your first table has all the key values, then you can do:
select t1.a, nz(t1.b, t2.b), nz(t1.c, t2.c) as c
from table1 as t1 left join
table2 as t2
on t1.a = t2.a;
If this isn't the case, you can use this rather arcane looking construct:
select t1.a, nz(t1.b, t2.b), nz(t1.c, t2.c) as c
from table1 as t1 left join
table2 as t2
on t1.a = t2.a
union all
select t2.a, t2.b, t2.c
from table2 as t2
where not exists (select 1 from table1 as t1 where t1.key = t2.key)
The first part of the union gets the rows where there is a key value in the first table. The second gets the rows where the key value is in the second but not the first.
Note this is much harder in Access than in other (dare I say "real") databases. MS Access doesn't support common table expressions (CTEs), unions in subqueries, or full outer join -- all of which would help simplify the query.
I am trying to do a multi query but I don't want to use sub queries i.e:
SELECT column1
FROM table1
WHERE
EXISTS (SELECT column1 FROM table2 WHERE table1.column1 = table2.column1);)
I thought of using a JOIN but so far my best result was this:
SELECT *
FROM table1
JOIN table2 ON table1.t1id = table2.t2id
WHERE table1.id = 5;
This would be good except of the fact that I get a duplicate column (the id in table 1 and 2 are foreign keys).
How do I remove the duplicate column if possible?
UPDATE:
Table1:
tableA_ID, TABLEB_ID
1, 1
1, 4
3, 2
4, 3
TableA: ID, COL1, COL2
1, A, B
2, A, B
3, A, B
4, A, B
TableB: ID, Col3, COL4
1, C, D
2, C, D
3, C, D
4, C, D
I want to get all or some of the columns from TableA according to a condition
Sample: Lets say the condition is that tableA_ID = 1 which will result in the 2 first rows in the table then I want to get all or some of the columns in TableA that respond to the ID that I got from Table1.
Sample: The result from before was [{1,1}{1,4}] which means I want from TableA the results:
TableA.ID, TableA.COL1, TableA.COL2
1,A,B
4,A,B
The actual results I get is:
Table1.tableA_ID, Table1.TABLEB_ID, TableA.ID, TableA.COL1, TableA.COL2
1,1,1,A,B
1,4,4,A,B
Is this what you're looking for?
select a.id, a.column1, b.column2
from table1 a
left join table2 b on a.id = b.otherid;
You can't change the column list of a query based on the values it returns. It just isn't the way that SQL is designed to operate. At best, you can return all of the columns from the second table and ignore the ones that aren't relevant based on other values in that row.
I'm not even sure how a variable column list would work. In your scenario, you're looking for two discrete values separately. But that's not the only scenario: what if the condition is tableA_ID in (1,2). Would you want different numbers of columns in different rows as part of a single result set?
Getting just the columns you want (just from specific tables, as you say) is the easy part (btw -- don't use '*' if you can help it -- topic for another discussion):
SELECT
A.ID,
A.COL1,
A.COL2
FROM
TABLE1 Bridge
LEFT JOIN TABLEA A
ON Bridge.TABLEA_ID = A.ID
LEFT JOIN TABLEB B
ON Bridge.TABLEB_ID = B.ID
Getting the rows you want will be the harder part (influenced by your choice of joins, among several other things).
I think you'll need to select only the fields of table A and use a distinct clause. Rest of your query will remain as it is. i.e.
SELECT distinct table1.*
FROM table1
JOIN table2 ON table1.t1id = table2.t2id
WHERE table1.id = 5;
My apologies in advance if this particular question has already been asked and answered ... there so many different particular ways the JOIN command is used that it can be difficult to find the exact answer to a given problem. I'm a SQL novice, so ... if the solution exists, feel free to point me to it.
I'm trying to join 3 different tables, and I believe what I want is equivalent to a FULL OUTER JOIN (not supported by MySQL, as I understand) on all 3 tables. Consider a Venn diagram with 3 circles; I want the full union of all 3 circles, including the full intersection, all three pair-wise joins (where one table returns NULLs), and all three single instances (where two tables return NULLs). I believe what I've got here will work, but it's brute-force and I'm sure there is a more efficient way. I'm also a bit concerned with my use of WHERE NOT EXISTS, so please correct me if necessary. Here's the gist of my code:
// Intersection of all three tables
SELECT [table1.cols], [table2.cols], [table3.cols]
FROM table1
INNER JOIN table2
ON table1.col1 = table2.col1
INNER JOIN table3
ON table1.col1 = table3.col1
UNION ALL
// Intersection of tables one and two
SELECT [table1.cols], [table2.cols], [NULLS]
FROM table1
INNER JOIN table2
ON table1.col1 = table2.col1
WHERE NOT EXISTS (table1.col1 = table3.col1)
UNION ALL
// Intersection of tables two and three
SELECT [NULLS], [table2.cols], [table3.cols]
FROM table2
INNER JOIN table3
ON table2.col1 = table3.col1
WHERE NOT EXISTS (table2.col1 = table1.col1)
UNION ALL
// Intersection of tables three and one
SELECT [table1.cols], [NULLS], [table3.cols]
FROM table3
INNER JOIN table1
ON table3.col1 = table1.col1
WHERE NOT EXISTS (table3.col1 = table2.col1)
UNION ALL
// Only in table one
SELECT [table1.cols], [NULLS], [NULLS]
FROM table1
WHERE NOT EXISTS ((table1.col1 = table2.col1))
AND NOT EXISTS ((table1.col1 = table3.col1))
UNION ALL
// Only in table two
SELECT [NULLS], [table2.cols], [NULLS]
FROM table2
WHERE NOT EXISTS ((table2.col1 = table1.col1))
AND NOT EXISTS ((table2.col1 = table3.col1))
UNION ALL
// Only in table three
SELECT [NULLS], [NULLS], [table3.cols]
FROM table3
WHERE (NOT EXISTS (table3.col1 = table1.col1))
AND (NOT EXISTS (table3.col1 = table2.col1))
TIA for your help, and your grace. :)
to simulate your full outer join, I would pre-query just the UNIQUE IDs you are EXPECTING, then LEFT JOIN to each other...
select
AllPossibleKeys.CommonID,
T1a.*,
T2a.*,
T3a.*
from
( select distinct T1.col1 as CommonID
from table1 T1
UNION
select distinct T2.col1 as CommonID
from table2 T2
UNION
select distinct T3.col1 as CommonID
from table1 T3 ) as AllPossibleKeys
LEFT JOIN table1 T1a
on AllPossibleKeys.CommonID = T1a.col1
LEFT JOIN table2 T2a
on AllPossibleKeys.CommonID = T2a.col1
LEFT JOIN table3 T3a
on AllPossibleKeys.CommonID = T3a.col1