Conditionally joining from multiple tables - sql

Does SQL allow some form of conditional "table choosing" within the Join statement? ie. selecting a different table to join based on a predefined variable/condition.
define var = 1
select *
from tbl
join (case when &var=1 then tblA when &var=2 then tblB else tblC end) a on tbl.id = a.id
The error I get when attempting this method is ORA-00905: missing keyword.

No. Neither SQL nor Oracle allow this, unless you use dynamic SQL.
Assuming the tables have the same columns, you could write this logic as:
select *
from tbl join
(select a.* from tblA where &var = 1 union all
select b.* from tblB where &var = 2 union all
select c.* from tblC where &var not in (1, 2)
) abc
on tbl.id = abc.id;

You still need to specify all joins ahead of time and they would have to be left outer joins, but you can rewrite your statement like this. This way will work regardless of the number of fields in each of the tables (requirement for the union), and if they are named differently then you can access the appropriate field by name.
DECLARE #var int
SET #var=1
select tbl.*, tblA.ShippingName, tblB.BillingName, tblC.OtherName
from tbl
left outer join tblA on tbl.id = tblA.id and #var = 1
left outer join tblB on tbl.id = tblB.id and #var = 2
left outer join tblC on tbl.id = tblC.id and #var = 3

Related

SQL conditional joins

I have a table-valued function with joins where I want to choose which join I use depending on a local variable like:
DECLARE #type int;
Then do some logic with #type and set it to 1.
SELECT ...
FROM table t
inner join ... a on a.id = t.id and #type = 1 -- Only trigger this join if #type is 1
inner join ... b on b.id = t.id and #type = 2 -- Only trigger this join if #type is 2
So my question is: how can I choose which join to trigger depending on the value of #type (if even possible).
The reason I want to do this is that the SELECT statement is massive, and I don't want repetitive code in the script.
Use left join instead:
SELECT ...
FROM table t LEFT JOIN
a
ON a.id = t.id AND #type = 1 LEFT JOIN
b
ON b.id = t.id AND #type = 2 ;
You might need WHERE #type IN (1, 2) if you want an empty result set for other values.
You will need COALESCE() in the SELECT to combine the columns:
COALESCE(a.col1, b.col1) as col1
This should be quite efficient. However, you might want to simply use UNION ALL:
SELECT ...
FROM table t JOIN
a
ON a.id = t.id
WHERE #type = 1
UNION ALL
SELECT ...
FROM table t JOIN
b
ON b.id = t.id
WHERE #type = 2 ;
You could union your two tables within a subquery. For any similar columns (i.e. would be in the same column in the outer select) you can place them above each other, for columns unique to each source you'd need to pad the other side of the union with NULL, e.g.
SELECT t.id,
a.SimilarCol,
a.UniqueToA,
a.UniqueToB
FROM Table AS t
INNER JOIN
( SELECT a.id,
a.SimilarCol, -- Column you would want to consider the same in each table
a.UniqueToA, -- Column Unique to this table
UniqueToB = NULL -- Column Unique to the other table
FROM SomeTable AS a
WHERE #Type = 1
UNION ALL
SELECT b.id,
b.SimilarCol,
UniqueToA = NULL,
b.UniqueToB
FROM SomeOtherTable AS b
WHERE #type = 2
) AS a
ON a.id = t.id;
Example on db<>Fiddle

Selective Join in View Sql

Here is pseudocode of what I am trying to achieve
Select * from TableA
if(TableA.criteria != 1)
inner join TableB.criteria = TableA.criteria
I must do it in view can use sp, functions, etc.
Thanks for any help you will be able to provide
Is this what you want?
Select a.*, b.* from TableA a join TableB b
on b.criteria = case when a.Criteria = 1
then 1 else null end
This may be what you want:
select a.*, b.*
from a left join
b
on b.criteria = a.criteria and b.criteria = 1;
The left join will keep all rows in a, even when the on condition is not true. The extra columns from b will be NULL, unless the criterias are both "1".

I cannot get this LEFT JOIN to work (I don't understand joins)

These queries both get results:
SELECT * FROM Table1 WHERE Criteria = '5'
SELECT * FROM Table1 WHERE Criteria = '3'
This query gets results:
SELECT *
FROM Table1 p, Table2 m
WHERE p.UID = m.ID
AND Criteria = '5'
This query does not:
SELECT *
FROM Table1 p, Table2 m
WHERE p.UID = m.ID
AND Criteria = '3'
I am trying to convert these to a proper join which returns results even if there are no records in the right table.
I have tried the following
SELECT *
FROM Table1 p LEFT JOIN Table2 m ON p.UID = m.ID
WHERE p.Criteria = '3'
AND m.OtherCriteria = 'Moron'
--0 results
My limited understanding was that LEFT join is what I needed. I want data from the left table even if there is no data in the right table that matches. Since this didn't work I also tried right join, left outer join, right outer join and full join. None returned results.
What am I missing?
This is too long for a comment. Your query:
SELECT *
FROM Table1 p LEFT JOIN
Table2 m
ON p.UID = m.ID AND p.Criteria = '3';
Should be returning a row for all rows in table1. If there is no match, then the values will be NULL for table2. This is easily demonstrated: Here is a MySQL example on SQL Fiddle. Because this is standard behavior, it should work on almost any database.
Note that this query is quite different from this one:
SELECT *
FROM Table1 p LEFT JOIN
Table2 m
ON p.UID = m.ID
WHERE p.Criteria = '3';
This query returns no rows, because no rows match the WHERE clause. The filtering happens (conceptually) after the LEFT JOIN.
I changed the code in the SQL Fiddle slightly, so that query is:
select *
from (select 5 as criteria, 1 as id union all
select 6, 1 union all
select 7, 2
) table1 left join
(select 1 as id, 'x' as x
) table2
on table1.id = table2.id and criteria = 3;
As a note: you should always use explicit join syntax. Simple rule: Never use commas in the FROM clause.
If your database is returning no rows, then it is behaving in a non-standard manner or your interface has decided to filter the rows for some reason.

Is it possible to use IF or CASE in sql FROM statement

I have a long stored procedure and I would like to make a slight modification to the procedure without having to create a new one(for maintenance purposes).
Is it possible to use a IF or CASE in the FROM statement of the select statement to join other tables?
Like this:
from tableA a
join tableB b a.indexed = c.indexed
IF #Param='Y'
BEGIN
join tableC c a.indexed = c.indexed
END
It didn't seem to work for me. But I am wondering if this is even possible and/or if this even makes sense to do.
Thanks.
No, it is not possible. You can only accomplish this through the use of dynamic SQL.
The Curse and Blessings of Dynamic SQL
An Intro to Dynamic SQL
I would not advise using Dynamic SQL, there are most likely better ways to perform this operation but you would have to provide more info.
You can achieve something like it if you have a left outer join
Consider
declare #param bit = 1
select a.*, b.*, c.* from a
inner join b on a.id = b.a_id
left outer join c on b.id = c.b_id and #param = 1
This will return all columns from a, b, c.
Now try with
declare #param bit = 0
This will return all columns from a and b, and nulls for columns of c.
It won't work if both joins are inner.
No this is not possible. Your best bet would probably be to select from both tables and only include the data your care about. If you provide an example of what you are trying to do I can provide a better answer.
Attempt at an example:
SELECT t1.id, COALESCE(t2.name, t3.name)
FROM Table1 as t1
LEFT JOIN Table2 as t2
ON t1.id = t2.id
LEFT JOIN Table2 as t3
ON t1.id = t3.id
While what you proposed is not possible, you can play with your where conditions:
from tableA a
inner join tableB b ON a.indexed = c.indexed
left join tableC c ON a.indexed = c.indexed AND 1 = CASE #Param WHEN 'Y' THEN 1 ELSE 0 END
More performant would be to just doing a big
IF #Param='Y' THEN
from tableA a
inner join tableB b ON a.indexed = c.indexed
ELSE
from tableA a
inner join tableB b ON a.indexed = c.indexed
left join tableC c ON a.indexed = c.indexed
You haven't revealed you SELECT clause. The essence of what you want is as follows:
SELECT indexed
FROM tableA
INTERSECT
SELECT indexed
FROM tableB
INTERSECT
SELECT indexed
FROM tableC
WHERE #Param = 'Y'
Then use this table expression as dictated by your SELECT clause e.g. say you only want to project tableA:
WITH T
AS
(
SELECT indexed
FROM tableA
INTERSECT
SELECT indexed
FROM tableB
INTERSECT
SELECT indexed
FROM tableC
WHERE #Param = 'Y'
)
SELECT *
FROM tableA
WHERE indexed IN ( SELECT indexed FROM T );

Query from 2 pseudotables, hosted in the same real table

Let there are 2 tables. To query the rows, which have the same IDs, you have to do this:
SELECT * FROM Table1 A, Table2 B WHERE A.id = B.id
Now let the tables be merged into one global table, with an added ex-table column. So, query
SELECT * FROM Table1
now looks like:
SELECT * FROM GlobalTable WHERE tableId = 1
But how the first query should look now?
SELECT * FROM Table1 A, Table2 B WHERE A.id = B.id
?
One table should store one entity. There is no such thing as a "one true lookup table" or "global table". Nor should you consider an EAV. This question assumes all your tables have the same layout...
However, I look forward to more rep later when it doesn't work properly so...
You should use explicit JOINs to separate filter and join conditions
Select *
from
GlobalTable A
JOIN
GlobalTable B ON A.id = B.id
WHERE
A.tableId = 1 AND B.tableId = 2
If you need to do an OUTER JOIN, then you can write this
Select *
from
(SELECT * FROM GlobalTable WHERE tableId = 1) A
LEFT JOIN
(SELECT * FROM GlobalTable WHERE tableId = 2) B ON A.id = B.id
I'd suggest using an indexed view though to persist "tableA" and "tableB" as separate objects to avoid this continual filtering. Or don't merge them...