I have a doubt in generating Query.
Which is best way in these two
Eg:1
DECLARE #SQLQuery varchar(MAX)
SET #SQLQuery='Select tab1.Name,tab2.Name From table1 INNER JOIN table2 ON table2.ID=table1.ID'
IF #Val=0
SET #SQLQuery=#SQLQuery+' where table1.ID>5'
ELSE
SET #SQLQuery=#SQLQuery+' where table2.ID>5'
Eg 2:
IF #Val=0
BEGIN
Select tab1.Name,tab2.Name
From table1
INNER JOIN table2
ON table2.ID=table1.ID
WHERE table1.ID>5
END
ELSE
BEGIN
Select tab1.Name,tab2.Name
From table1
INNER JOIN table2
ON table2.ID=table1.ID
WHERE table2.ID>5
END
Select tab1.Name,tab2.Name
From table1
INNER JOIN table2
ON table2.ID=table1.ID
WHERE (#Val=0 AND table1.ID>5) OR (#Val!=0 AND table2.ID>5)
IF #Val = 0
BEGIN
Select T1.Name From
(
Select Name From table1 Where Id > 5
)T1
INNER JOIN table2 T2 ON T2.ID = T1.ID
END
ELSE
BEGIN
Select T2.Name From
(
Select Name From table2 Where Id > 5
)T2
INNER JOIN table1 T1 ON T1.ID = T2.ID
END
In this way, We should first filter the values and then join between them.
Related
currently I have 3 tables
Table 1:
tb1_id
values
1
4
Table 2:
tb1_id
tbl3_id
1
5
Table 3:
tb3_id
values
5
2
For some reason the values from table 1 are not the same in table 3 (as shown above), I need to update the values of the table 1 with the table 3, but I am not able to do so, so far this is my query:
UPDATE
table1 t1
SET
values = temp_tbl.values
FROM
(
SELECT t2.tb1_id, t3.values FROM table2 t2
JOIN
table3 t3 ON t2.tbl3_id = t3.tbl3_id
) temp_tbl
WHERE
t1.tbl1_id = temp_tbl.tbl1_id
AND
t1.values != temp_tbl.values;
OK so if I understood you correctly, you just need to make a slight correction to your code:
UPDATE
table1 t1
SET
values = temp_tbl.values
FROM
(
SELECT t2.tbl1_id, t3.values
FROM table2 t2
Inner JOIN table3 t3 ON t2.tbl3_id = t3.tbl3_id
) temp_tbl
WHERE
t1.tbl1_id = temp_tbl.tbl1_id
AND
t1.values != temp_tbl.values;
Here is DBFiddle link.
EDIT: This would also do:
with newData as (
select t1.tbl1_id, t3.Values
from Table1 t1
inner join Table2 t2 on t1.Tbl1_Id = t2.Tbl1_Id
inner join Table3 t3 on t2.Tbl3_Id = t3.Tbl3_Id
where t1.Values != t3.Values
)
UPDATE
table1
SET
values = newData.Values
FROM newData
where table1.tbl1_id = newData.tbl1_id;
this is how you can do it:
-- temporary tables
SELECT 1 TBL1_ID, 4 valuet1 INTO #t1
SELECT 1 TBL1_ID, 5 TBL3_Id INTO #t2
SELECT 5 TBL3_ID, 2 valuet3 INTO #t3
-- select with joins
select T1.*,T2.*, T3.*
from #t1 T1
INNER JOIN #t2 T2
ON T1.TBL1_ID = T2.TBL1_ID
INNER JOIN #t3 T3
ON T3.TBL3_ID = T2.TBL3_ID
-- update would be like this
update #t1 set valuet1 = T3.valuet3
from #t1 T1
INNER JOIN #t2 T2
ON T1.TBL1_ID = T2.TBL1_ID
INNER JOIN #t3 T3
ON T3.TBL3_ID = T2.TBL3_ID
I have following query-
select * from
Table1 t1 , table2 t2 ,
(select idCol from table3) t3
My question is - Can I use table t2 inside subquery?
like this
select * from
Table1 t1 , table2 t2 ,
(select idCol, t2.nameCol from table3) t3
Obviously this gives error invalid identifier t2.nameCol
But if I write as follows, it give unnecessaru extra rows
select * from
Table1 t1 , table2 t2 ,
(select idCol, t2.nameCol from table3, table2 t2) t3
any other way to do this?
EDIT
Basically what I am trying to achieve is following
select * from
Table1 t1 , table2 t2 ,
(select
case
when t2.nameCol = 'ABC' then 'ABC'
else idCol
end idCol from table3) t3
To join a table only when certain criteria is met is called an outer join. Here is how:
select *
from table1 t1
inner join table2 t2 on <join criteria here>
left outer join table3 t3 on t2.namecol <> 'ABC' and <join criteria here>;
It may suffice in your case, however, to move the subquery to your SELECT clause:
select
t1.*,
t2.*,
case when t2.namecol = 'ABC' then
'ABC'
else
(select idcol from table3 t3 where <join criteria here>)
end
from table1 t1
inner join table2 t2 on <join criteria here>;
You can try below. I am sure you have some kind of join between t1 and t2. IN such case, you can add same at the end of the statement.
SELECT t1.*,
t2.*,
( SELECT CASE
WHERE t2.nameco1 = 'ABC' THEN 'ABC'
ELSE idcol
END idcol
FROM table3) idcol
FROM table1 t1,
table2 t2
I am having a scenerio like i am having two table with the name tbl1 and tb2
Select * from tbl1 t1 inner join tb2 t2 on t1.Id = t2.id
The above sample query here i need to achive is that i need to perform this inner join based on the condition. (i.e) I will pass input parameter IsJoin as true means, i can perform inner join else not. I am not preferring dynamic query. Kindly let me know if there is any other way to achieve this.
Thanks in advance
There's just one way to do that then - using IF and ELSE statements
IF <YOUR CONDITION>
BEGIN
SELECT *
FROM tbl1 t1
INNER JOIN tb2 t2
ON t1.Id = t2.id;
END
ELSE
BEGIN
SELECT *
FROM tbl1 t1;
END
If it's just few conditions, then it's fine to do this using multiple statements, however if it's tons of joins based on conditions - it's easier to maintain Dynamic SQL. And it becomes much more readable.
Try this way:
declare #IsJoin numeric(1,0)
select #IsJoin = 1
Select * from tbl1 t1
left join tb2 t2 on t1.Id = t2.id
and #IsJoin = 1
If you need to preserve all columns of t1 and t2 whether join or not
DECLARE #IsJoin bit = 1 -- 1 for true; 0 for false
SELECT
*
FROM
tbl1 t1
LEFT OUTER JOIN tbl2 t2 on t1.Id = t2.id and #IsJoin = 1
WHERE
(#IsJoin = 0 OR t2.id is not null) -- Keep the inner join behavior
Table1 (ID, Col1, col2, col3, col4)
Table2 (ID, Col5, col6)
-----------------SP-------------------
set #Ids = '1,2,3,4,5,6,7,8,9' // Input paremeter (can be NULL)
Create Table #Tabel2
(
Id int
)
Insert into #table2 select * from dbo.Split(#Ids, ',')
NOW #table2 has: 1 2 3 4 5 6 7 8 9
Scenario
Select t1.Col1,t1.col2,t1.col3
FROM Table1 as t1
INNER JOIN Table2 as t2 ON t1.Col1=t2.Col2
AND (#Ids is null OR t1.ID in (Select Id from #Table2))
Question
How to replace IN with conditional inner join?
Because you used alias t which was never defined in your query, it is not clear to me which table ID should be joined on. I took a guess and used t1.
Select t1.Col1,t1.col2,t1.col3
FROM Table1 as t1
INNER JOIN Table2 as t2 ON t1.Col1=t2.Col2
left outer join #Table2 ids on #Ids is null or t1.ID = ids.ID --maybe this should be t2.ID = ids.ID?
Unless I'm misunderstanding (it is friday afternoon after all) you want all rows from Table1/Table2, matched to #Table2 where possible, but everything if #Table2 is empty - this is simply an outer join.
SELECT t.Col1,
t.col2,
t.col3
FROM Table1 AS t1
INNER JOIN Table2 AS t2
ON t1.Col1 = t2.Col2
LEFT OUTER JOIN #Table2 t3
ON t2.ID = t3.ID
In SQL Server 2008, you can use CASE statements in your join clause, for example:
SELECT *
FROM table1
INNER JOIN table2 ON CASE WHEN table1.column1 = 'this' THEN null ELSE 'that' END = table2.column1
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;