Do Left or Inner Join depends on parameter - sql

I have to do 'left' or 'inner' join operation between tables depends on input parameter in my stored procedure. I know how to do it very simply:
if flag = 0
begin
select t1.*, t2.* from t1
inner join t2 on t2.id=t1.id
end
else
begin
select t1.*, t2.* from t1
left join t2 on t2.id=t1.id
end
Is there any more solution ? Thank you

you can do it using only left join by doing something like this
select t1.*, t2.* from t1
left outer join t2 on t2.id=t1.id
WHERE flag = 1 OR t2.id IS NOT NULL

You didn't mention the language, so maybe something like this:
select t1.*, t2.* from t1
left join t2 on t2.id=t1.id
if flag = 0
begin
where t2.id is not null
end

select t1.*, t2.*
from t1
inner join t2 on t2.id = t1.id
where flag = 0
UNION
select t1.*, t2.*
from t1
left outer join t2 on t2.id = t1.id
where coalesce(flag, 1) <> 0;

Related

How to get Top 1 with Sort from Join

Instead of doing something like this:
SELECT
t1.*,
(SELECT TOP 1 t2.Name FROM Table2 t2 Order By t2.Number) Val1
FROM Table1 t1
How would it be done with a Join instead?
SELECT
t1.*,
TOP 1 t2.Name
FROM Table1 t1
JOIN Table2 t2 Order By t2.Number
or is it even possible?
select top 1 * from (SELECT t1.*, t2.Number num FROM table1 t1 JOIN table2 t2 on t1.commoncolumn=t2.commoncolumn ) t order by t.num
Try this (id is the join field)
with Table2Data as
(
SELECT Id , Name ,row_number() over(partition by Id order by number) rowNumber FROM Table2
)
select t1.* ,t2.name from table1 t1
inner join Table2Data t2 on t1.Id=t2.Id and t2.rowNumber=1
The following accomplishes the same thing.
SELECT t1.*, MIN(t2.Name) OVER() val1 FROM Table1 INNER JOIN Table2 t2 on t1.id = t2.id
It can be done with a CROSS APPLY. Assuming Id is the common field:
SELECT t1.*
, t3.Name
FROM Table1 t1
CROSS APPLY (
SELECT TOP 1 t2.Name
FROM Table2 t2
WHERE t1.Id = t2.Id
ORDER BY t2.Number
) t3

Convert to join query

select t.* from table1 t where t.id NOT IN(
select Id from t2 where usrId in
(select usrId from t3 where sId=value));
I the result i need is like if there are matching id's in t1 and t2 then those id's should be omitted and only the remaining rows should be given to me. I tried converting into join but it is giving me the result i wanted. Below is my join query.
SELECT t.* FROM table1 t JOIN table2 t2 ON t.Id <> t2.Id
JOIN table3 t3 ON t3.Id=t2.Id WHERE t3.sId= :value
This doesn't feth me the correct result. it was returning all the rows, but i want to restrict the result based on the matching id's in table t1 and table t2. Matching id's should be ommited from the result.I will be passing the value for sId.
I believe this to be an accurate refactor of your query using joins. I don't know if we can do away with the subquery, but in any case the logic appears to be the same.
select t1.*
from table1 t1
left join
(
select t2.Id
from table2 t2
inner join table3 t3
on t2.usrId = t3.usrId
where t3.sId = <value>
) t2
on t1.Id = t2.Id
where t2.Id is null
Let's break down and solve problem step by step.
So your query
select t.* from table1 t where t.id NOT IN(
select Id from t2 where usrId in
(select usrId from t3 where sId=value));
on converting the inner query to JOIN will yield
select t.* from table1 t where t.id NOT IN
(SELECT T2.ID FROM T2 JOIN T3 on T2.UsrID =T3.UsrID and T3.sID=value)
which on further converting to JOIN with outer table will be
select t.* from table1 t LEFT JOIN
(SELECT T2.ID FROM T2 JOIN T3 on T2.UsrID =T3.UsrID and T3.sID=value)t4
ON t.id =T4.ID
WHERE t4.ID is NULL
In case you completely want to remove sub-query you can try like this
SELECT t.*
FROM table1 t
LEFT JOIN T2
ON T.ID=T2.ID
LEFT JOIN T3
ON T3.UsrId=T2.UsrID AND T3.sId=value
WHERE T3.UsrID IS NULL

postgresql join query

I have two tables T1 and T2
T1
-id
-columnA
-columnB
-columnC
T2
-id
-columnX
-columnY
-columnZ
I have a query like
Select t1.* t2.columnZ
from T1 t1
left join on T2 t2 on t1.id = t2.id
where t2.columnZ = 'test'
I want result like if "where t2.columnZ = 'test'" does not return any row then it should return value of columnZ as null value
Try:
SELECT t1.*
, t2.columnZ
FROM T1 t1
LEFT JOIN T2 t2
ON t1.id = t2.id
AND t2.columnZ = 'test'
You also missed a comma, and you had a misplaced on.

Oracle SQL: JOIN by dblink

I use next SQL-query in Oracle DB:
SELECT T1.*,
T3.*
FROM MyTable1 T1
INNER JOIN MyTable2 T2 ON T2.Id1 = T1.Id
LEFT JOIN MyTable3#dblink1 T3 ON T3.Id2 = T2.Id
This query is very simple and fast (about 1 min, T1 contain about 1 million rows, T3 more then 10 million rows). Now I want to use MyTable4 from dblink1 for filtering selected rows data. For it, I use subquery:
SELECT T1.*,
T3.*
FROM MyTable1 T1
INNER JOIN MyTable2 T2 ON T2.Id1 = T1.Id
LEFT JOIN (SELECT Sub_T1.*
FROM MyTable3#dblink1 Sub_T1
INNER JOIN MyTable4#dblink1 Sub_T2 ON Sub_T2.Id3 = Sub_T1.Id
WHERE
Sub_T2.MyColumn1 = 'required value') T3 ON T3.Id2 = T2.Id
But this query is too slow (more then 20min). If I rewrite this query to:
SELECT T1.*,
T3.*
FROM MyTable1 T1
INNER JOIN MyTable2 T2 ON T2.Id1 = T1.Id
LEFT JOIN MyTable3#dblink1 T3 ON T3.Id2 = T2.Id
LEFT JOIN MyTable4#dblink1 T4 ON T4.Id3 = T3.Id
WHERE
T4.MyColumn1 = 'required value'
Then my query work fast again, but I donn't like result (I want to see columns of T3 as null, if WHERE return false).
How to improve my second query, for speed up it?
Does phrasing the query with parentheses solve the problem?
SELECT T1.*,
T3.*
FROM MyTable1 T1 INNER JOIN
MyTable2 T2
ON T2.Id1 = T1.Id LEFT JOIN
(MyTable3#dblink1 T3 JOIN
MyTable4#dblink1 T4
ON T4.Id3 = T3.Id AND
T4.MyColumn1 = 'required value'
)
ON T3.Id2 = T2.Id;
Or, also:
SELECT T1.*,
T3.*
FROM MyTable1 T1 INNER JOIN
MyTable2 T2
ON T2.Id1 = T1.Id LEFT JOIN
MyTable3#dblink1 T3
ON T3.Id2 = T2.Id
EXISTS (SELECT 1 FROM MyTable4#dblink1 T4 WHERE T4.Id3 = T3.Id AND T4.MyColumn1 = 'required value'
)

Using an inner join with subqueries in an update syntax

I am trying to use a inner join with an update statement with a subquery ... can you help me out with the sytax please --- and also how do you use the AS clause for alias in sql server???
the following is what i am trying to do :
Update Table1
inner join table2
set table1.value1 = (select table2.value1 where table1.value 1 ....)
any idea??
If you need to use a subquery to perform the UPDATE you can do it this way:
UPDATE t1
SET t1.value = t2.value
FROM Table1 t1
JOIN
(
SELECT id, value
FROM table2
) t2
ON t1.id = t2.id
One way is to alias the table:
update t1
set table1.value1 = t2.value1
from table1 as t1
join table2 as t2
on t1.id = t2.t1_id
You should try
UPDATE table1 SET t1.value1 = t2.value2
FROM table1 t1
INNER JOIN table2 t2
ON t1.field1 = t2.field2
UPDATE Table1 t1
INNER JOIN (
SELECT id, value
FROM table2
) t2 USING(id)
SET t1.value = t2.value