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';
Related
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 a query like below (SQL Server 2008):
if (#checkValue = 1)
begin
select *
from table1 t
inner join table2 t2 on t.Id = t2.t_Id
and DATEDIFF(hour,t2.Start,t.Start) < 24
end
else
begin
select *
from table1 t
inner join table2 t2 on t.Id = t2.t_Id
and Convert(Date, t.Start) = Convert(Date,t2.Start)
end
I am not happy with this query since I basically duplicated the main query and made a slight change. I am looking for a more professional way, like merging the queries into 1 by adding a where clause maybe. I appreciate if someone helps. Thanks.
You can try this with the conditions specified in a where clause.
select *
from table1 t
inner join table2 t2 on t.Id = t2.t_Id
where (#checkValue = 1 AND DATEDIFF(hour,t2.Start,t.Start) < 24)
or (#checkValue <> 1 AND Convert(Date, t.Start) = Convert(Date,t2.Start))
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've two tables T1 and T2 with the following columns -
T1
Project_ID
Category
Column_X
Column_Y
Column_Z
T2
Proj_ID
Category
Parent_Project_ID
I want to write a query to get records from T1 with the following condition -
Get Projects with Category = "A" from T1
Get child projects of the above filtered projects
I'm not sure how to check the second condition only with the results coming out of first condition.
What is needed?
Projects from T1 where Category is A
Child projects of projects obtained from condition 1
Adding sample data and desired results as requested -
To get all records from second table then you can use the following query.
SELECT
t2.*
FROM T1 t1
RIGHT OUTER JOIN T2 t2 ON t1.Project_ID = t2.Project_ID
WHERE t1.Category = "A"
SELECT * FROM T2 WHERE T2.Proj_ID IN ( SELECT Project_ID FROM T1 WHERE Category = 'A' )
This should do the job needed.
SELECT * from T2 as d
WHERE EXISTS ( SELECT * from T1 as d1 where d1.Category = 'A' and d1.Project_ID = d.Proj_ID )
SELECT * from T1 as d1 right join T2 as d2 on d1.Project_ID = d2.Proj_ID
WHERE d1.CodTert = 500
I've made an update, these query give the same result, one uses the JOIN one doesn't.
I'm assuming that T2.Parent_Project_ID and T1.Project_ID are related. If so, you can use this:
Select T3.*
From T1
Join T2 On T2.Parent_Project_ID = T1.Project_ID
Join T1 T3 On T3.Project_ID = T2.Proj_ID
Where T1.Category = 'A'
This would get only child projects of projects that have a category of 'A'.
EDIT:
Based on the output format that has been added to the question, the following query, which uses a LEFT OUTER JOIN would render the exact result required:
SELECT
T2.PROJ_ID Project_ID,
T2.Category,
T1.Column_X,
T1.Column_Y,
T1.Column_Z,
T2.Parent_Project_ID
FROM T1 T1_PARENTS
INNER JOIN T2
ON T2.Parent_Project_ID = T1.Project_ID and T1.Category = 'A'
INNER JOIN T2 T2_CHILDREN
ON T2_CHILDREN.PROJ_ID = T2.Parent_Project_ID OR T2_CHILDREN.Parent_Project_ID = T2.Parent_Project_ID
LEFT OUTER JOIN T1
ON T2_CHILDREN.PROJ_ID = T1.Project_ID;
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.