I'm using a select statement inside a stored procedure. All I need to do is based on the value of a parameter I've to use either RIGHT JOIN or INNER JOIN. Please anyone there help me to achieve this. Many Thanks in Advance..
SELECT FLD1, FLD2
FROM TBL1 C (NOLOCK)
CASE
WHEN #SHOW = 156 THEN INNER
ELSE RIGHT JOIN TBL2 IC (NOLOCK) ON C.FLD3 = IC.FLD4
END
Is it correct? What would be the right way of doing this?
You could use IF statements:
DECLARE #Param VARCHAR(10)
SET #Param = 'RIGHT'
IF (#Param = 'RIGHT')
SELECT t1.Something
FROM Table1 t1
RIGHT JOIN Table2 t2 ON t1.ID = t2.ID
WHERE ...
ELSE IF (#Param = 'INNER')
SELECT t1.Something
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.ID = t2.ID
WHERE ...
Or could be done as one SELECT (not necessarily best performance, be sure to test), something like:
SELECT t1.Something
FROM Table1 t1
RIGHT OUTER JOIN Table2 t2 ON t1.ID = t2.ID
WHERE (#Param = 'RIGHT') OR (#Param = 'INNER' AND t1.ID IS NOT NULL)
Theres more than enough information on the case statement out there, this should get you started.
Related
I have an sql update statement:
update table1 set col1='val' where id in (select t2.id
from table2 t2
inner join table1 t1 on t1.id = t2.id
where t1.col2='val2' and t2.col3='val3');
Is there a better way to write it? I am thinking somehow not to use join since I have the table in the update construction.
Maybe:
update table1 set col1='val' where id in (select t2.id
from table2 t2
where t2.col3='val3')
and t1.col2='val2';
Is it better the last query?
Maybe with exists() ?
UPDATE table1 s
SET s.col1='val'
WHERE EXISTS(SELECT 1 FROM table2 t
WHERE s.id = t.id AND someCondition)
Initially, I have a query like below, doing a join on 1=1. (It's simply doing a cross join, which selects all rows from the first table and all rows from the second table and shows as a cartesian product, i.e. with all possibilities.)
SELECT * FROM Table1 t1
JOIN Table2 t2 ON 1=1
Problem: Optimize this query in such a way, it will show only the records for a particular ID and if we don't have an ID or have a NULL in the ID then it will show the result same as previously(1=1). So I wrote the script below.
Declare #T2id as int;
Set #T2id = 123;
SELECT * FROM Table1 t1
JOIN Table2 t2 ON
-- left side of join on statement
CASE
WHEN #T2id Is NULL
THEN 1
ELSE
t2.Id
END
=
-- right side of join on statement
CASE
WHEN #T2id Is NULL
THEN 1
ELSE
#T2id
END
Can anyone confirm, is it good or we can have a better approach than this?
I think your way of presenting a cross-join is something I haven't seen before.
My view is it's simpler to read and understand if you just:
SELECT *
FROM Table1 t1, Table2 t2
As for the question, assuming SQL Server (you didn't tag the RDBMS, but I guess from your variable declaration) you might consider:
IF ISNULL(#T2id,1) = 1
SELECT *
FROM Table1 t1, Table2 t2;
ELSE
SELECT *
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.id = t2.id
WHERE t2.id = #T2id;
I have the following query
SELECT
#EnrollmentTime = T1.EnrollmentTime
FROM
T1
INNER JOIN
T2 ON T1.DeviceMacAddress = T2.DeviceMacAddress
WHERE
T1.ID = #LocationID
I want to select this query only when this scenario exists else select
something.
How to achieve this with minimal lines of code
Simply you can use ISNULL as below
SELECT #EnrollmentTime=ISNULL(#EnrollmentTime, T1.EnrollmentTime)
FROM T1
INNER JOIN T2 ON T1.DeviceMacAddress=T2.DeviceMacAddress
WHERE T1.ID=#LocationID
This may help you
IF (#EnrollmentTime IS NOT NULL) OR (LEN(#EnrollmentTime) > 0)
SELECT #EnrollmentTime=T1.EnrollmentTime
FROM T1 INNER JOIN T2 ON T1.DeviceMacAddress=T2.DeviceMacAddress WHERE
T1.ID=#LocationID
ELSE
PRINT 'else';
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
I am using SQL Server 2005. I am trying to join 2 tables together, but only when a column value in the main table is true. Like this:
select * from T1
join T2 on T1.value = T2.value
where T2.value2 = 'variable2'
and T2.value3 = 'variable3'
There is a column value in T1 which says if I have to use the values in T2. I could to a case around the where clause, but it will always join to the table, and if the value in T1 is false, there are no values in T2 to join to, so the select returns no rows.
You can't put a case around the join, so I am a little bit stuck with this ... can anyone help ?
select *
from T1
join T2
on T1.value = T2.value
and T1.booleancolumn = 1
where T2.value2 = 'variable2'
and T2.value3 = 'variable3';
Similar to what Dave posted, but I also read that to mean you want to actually substitute values in the results. In that case:
SELECT
COALESCE(T2.Value, T1.Value) AS Value,
COALESCE(T2.Value2, T1.Value2) AS Value2,
COALESCE(T2.Value3, T1.Value3) AS Value3
FROM T1
LEFT JOIN T2 ON T2.value = T1.value
AND T2.Value2= #Variable2 AND T2.Value3 = #Variable3
Note that I'm treating your constants as real variables, too.
Dave Marke is right, but more generaly...
conditional join is answer to your question, try find some resources on web for example
http://weblogs.sqlteam.com/jeffs/archive/2007/04/03/Conditional-Joins.aspx
http://bytes.com/groups/ms-sql/641690-difference-condition-join-where
it is very powerful technique to make joins depending on data in tables
Perhaps I have misunderstood your question, but here's my guess.
I think you can use a case in the WHERE.
select * from T1
join T2 on T1.value = T2.value
where T2.value2 = case T1.useT2 when 'yes' then 'variable2' else T2.value2 END
and T2.value3 = case T1.useT2 when 'yes' then 'variable3' else T2.value3 END
Very similar to what Dave said, although I would put the booleancolumn check into the WHERE so something like this
SELECT * FROM T1
INNER JOIN T2
ON T1.Value = T2.Value
WHERE T2.Value2 = 'variable2'
AND T2.Value3 = 'variable3'
AND T1.booleancolumn = 1
What, if any, are the performance considerations related to conditional joins, I wonder? Is it equivalent to a UNION like this:
SELECT T1.*
FROM T1
WHERE T1.use_t2 = FALSE
AND T1.value2 = 'variable2'
AND T1.value3 = 'variable3'
UNION ALL
SELECT T2.*
FROM T1 JOIN T2 ON T1.value = T2.value
WHERE T1.use_t2 = TRUE
AND T2.value2 = 'variable2'
AND T2.value3 = 'variable3'