I would like to make the following join, which is some sort of UNION ALL in SQL. How to do it? Preferably how to do it by selecting all columns form t1 and t2 and declaring matching keys? I would like to avoid specifying column names if it is possible.
Update. I put some values in columns to be more specific. This is what I want:
Join 1
Probably I got answers for this:
Join 2
Use a full outer join, like so:
select *
from table1 t1
full outer join table2 t2
on t1.c4 = t2.c1 and t1.c5 = t2.c2
While SQL Server supports full outer joins, MySQL does not. This query can be rewritten in that situation as follows:
select *
from table1 t1
left outer join table2 t2
on t1.c4 = t2.c1 and t1.c5 = t2.c2
union
select *
from table1 t1
right outer join table2 t2
on t1.c4 = t2.c1 and t1.c5 = t2.c2
Based on your updated requirements, the form of this join specified above can be used with slight modifications like so:
select null,null,null,t.* from table1 s
right outer join table2 t on s.c4 = t.c1 and s.c5 = t.c2
union
select s.*,null,null from table1 s
left outer join table2 t on s.c4 = t.c1 and s.c5 = t.c2
Note that you will still need to include the literal value null in your select clause, once for each column that needs to be defaulted to null.
Demo
Use a FULL OUTER JOIN
Assuming C4 and C5 in your output example are from t2...
SELECT t1.C1, t1.C2, t1.C3, t2.C3, t2.C4
FROM t1
FULL OUTER JOIN t2 ON t1.C4 = t2.C1 AND t1.C5 = t2.C2
Related
This is somewhat of a followon to SQL JOIN where to place the WHERE condition?
I would prefer to use the USING clause on the join, but am not yet able to put the field value condition with the join.
SELECT 1
FROM table1 t1
JOIN table2 t2 USING(id) AND t2.field = 0;
ORA-00933: SQL command not properly ended
Is it possible to have USING and another condition as part of the JOIN clause?
You can use:
SELECT 1
FROM table1 t1
JOIN table2 t2 USING(id)
WHERE t2.field = 0;
Using USING(id) is like using ON t1.id = t2.id except that in the JOIN result instead of two columns t1.id & t2.id there is only one id column.
For INNER JOIN USING with a condition followed by an OUTER JOIN you need a subquery to keep the WHERE with the USING:
SELECT ...
FROM (SELECT id, ...
FROM table1 t1
JOIN table2 t2 USING(id)
WHERE t2.field = 0) s
LEFT JOIN ...;
For an OUTER JOIN USING with a condition you need a subselect:
SELECT ...
FROM table1 t1
LEFT JOIN (SELECT *
FROM table2 t2
WHERE t2.field = 0) t2
USING (id);
See this re ON/WHERE with JOIN. See this re ON/WHERE when mixing INNER & OUTER JOINs.
I have the following query and I am getting a multi-part identifier could not be bound exception. I realize why and my next thought was to use a CROSS APPLY, which didn't work. What I really need to do is join that subquery to make sure I get the most recent entry for a particular column.
Unfortunately, I've run out of ideas... How do I accomplish this query?
SELECT t1.*
FROM dbo.Table1 t1 (nolock)
INNER JOIN
(
SELECT TOP 1 t2_s.c1, t2_s.c2, t2_s.c4
FROM dbo.Table2 t2_s
WHERE t2_s.c1 = t1.c1
ORDER BY t2_s.dateStamped DESC
) t2
on t2.c1 = t1.c1
INNER JOIN dbo.Table3 t3
on t3.c1 = t2.c2 and t3.c2 = 1
WHERE t1.c2 = 'xxx'
Assuming you are using SQL Server, then CROSS APPLY should do what you want:
SELECT t1.*
FROM dbo.Table1 t1 (nolock) CROSS APPLY
(SELECT TOP 1 t2_s.c1, t2_s.c2, t2_s.c4
FROM dbo.Table2 t2_s
WHERE t2_s.c1 = t1.c1
ORDER BY t2_s.dateStamped DESC
) t2 INNER JOIN
dbo.Table3 t3
on t3.c1 = t2.c2 and t3.c2 = 1
WHERE t1.c2 = 'xxx';
I want to use the result of a FULL OUTER JOIN as a table to FULL OUTER JOIN on another table. What is the syntax that I should be using?
For eg: T1, T2, T3 are my tables with columns id, name. I need something like:
T1 FULL OUTER JOIN T2 on T1.id = T2.id ==> Let this be named X
X FULL OUTER JOIN T3 on X.id = t3.id
I want this to be achieved so that in the final ON clause, I want the T3.id to match either T1.id or T2.id. Any alternative way to do this is also OK.
SELECT COALESCE(X.id,t3.id) AS id, *-- specific columns here instead of the *
FROM
(
SELECT COALESCE(t1.id,t2.id) AS id, * -- specific columns here instead of the *
FROM T1 FULL OUTER JOIN T2 on T1.id = T2.id
) AS X
FULL OUTER JOIN T3 on X.id = t3.id
Often, chains of full outer joins don't behave quite as expected. One replacements uses left join. This works best when a table has all the ids you need. But you can also construct that:
from (select id from t1 union
select id from t2 union
select id from t3
) ids left join
t1
on ids.id = t1.id left join
t2
on ids.id = t2.id left join
t3
on ids.id = t3.id
Note that the first subquery can often be replaced by a table. If you have such a table, you can select the matching rows in the where clause:
from ids left join
t1
on ids.id = t1.id left join
t2
on ids.id = t2.id left join
t3
on ids.id = t3.id
where t1.id is not null or t2.id is not null or t3.id is not null
You can do it like you suggested, using IN()
FROM T1
FULL OUTER JOIN T2
ON(T1.id = T2.id)
FULL OUTER JOIN T3
ON(T3.ID IN(T2.id,T1.id))
or I think you can do it with a UNION (depends on what you need) :
SELECT * FROM
(SELECT name,id from T1
UNION
SELECT name,id from T2) x
FULL OUTER JOIN T3
ON(t3.id = x.id)
I'm struggling to figure out how I can add a convenience data column to a join table to use for evaluation.
Sorry if the terminology is incorrect, I'm a bit of an SQL newbie.
For this query structure:
SELECT t1.id
FROM
table1 t1
INNER JOIN
table2 t2 ON t1.c2 = t2.c1
WHERE
t1.c5=5;
...I would like to add a column in the join table that is calculated using a function of a few of t1s columns. For example, the sum of t1.x, t1.y, t1.z in a variable called score that can then be referenced in the WHERE clause.
The reason behind wanting to do this is it will be referenced multiple times and will reduce verbosity and help readability.
I presume it will look something like:
SELECT t1.id
FROM
table1 t1
INNER JOIN
table2 t2 ON t1.c2 = t2.c1
-- function(t1.x, t1.y, t1.z) as score
WHERE
t1.c5=5;
--AND score ...
Using PostgreSQL 9.3
If the score is calculated from the t1 columns, then use a subquery on t1:
SELECT t1.id
FROM (select t1.*, function(t1.x, t1.y, t1.z) as score
from table1 t1
) t1 INNER JOIN
table2 t2
ON t1.c2 = t2.c1
WHERE t1.c5 = 5 AND
t1.score . . .
A good option would be to use the having clause.
Select t1.id, SUM(t1.x + t1.y + t1.z)
FROM table1 t1 inner join table2 t2 on t1.c2 = t2.c1
WHERE t1.c5 = 5
GROUP BY t1.id
Having SUM(t1.x + t1.y + t1.z) = ...
Or, you could do a sub-query in the from clause. The below code makes some assumptions on structure.
SELECT t1.id
FROM
(Select SUM(t1.x + t1.y + t1.z) as score, t1.c5, t1.c2, t1.id
FROM table1 t1
GROUP BY t1.id
) t1
INNER JOIN table2 t2 on t1.c2 = t2.c1
WHERE
t1.c5=5
And t1.score = ...
I have three table I need to query table 1,2 and 3 and select C1 from Table 1 and C2 from table 2 and 3 respectively so that it would give result. Is there any way of getting this?
Yes:
select t1.c1, coalesce(t2.c2, t3.c2) c2
from Table1 t1
left join Table2 t2 on t1.c1 = t2.c1
left join Table3 t3 on t1.c1 = t3.c1
Try the following:
SELECT T1.C1, T2.C2
FROM T1
INNER JOIN T1.C1 = T2.C1
UNION ALL
SELECT T1.C1, T3.C2
FROM T1
INNER JOIN T1.C1 = T3.C1