SQL Server INNER JOIN according to condition - sql

I would like to add INNER JOIN if and only if a variable of my T-SQL function isn't null and not empty like that:
CREATE FUNCTION [dbo].[MyFunction]
(#FrequencyFilter VARCHAR(10)) -- EXAMPLE '6;1;4'
SELECT field1, field2, field3
FROM Table1 AS T1
JOIN Table2 AS T2 ON T2.id = T1.t2Id
IF #FrequencyFilter IS NULL OR EMPTY
JOIN ((STRING_SPLIT(#FrequencyFilter , ';') AS FF ON FF.Value = T1.frequency
WHERE T1.field4 = 'test'
I would like to only apply this join (none an outer), only if the frequency filter parameter isn't null and not empty

Related

SQL - Use different Select Statements based on column value

For instance,
Select field1
From table1
when table1.field1 = 'S'
then (select field1,2,3,4,5,6,.....
form table1,2,3,4,5,6,....(with joins))
when table1.field1 = 'O'
then (select field1,2,3,4,5,6,.....
from table1,2,3,4,5,6,.....(with join))
I think I got what you need. One possible solution is to create a view with hardcoded where clauses on it. This is the idea:
CREATE VIEW ConditionalSelect AS
SELECT ...fields...
FROM ...tables...
JOIN ...joins...
WHERE table1.field1 = 'S'
AND ....
UNION
SELECT ...fields...
FROM ...tables...
JOIN ...joins...
WHERE table1.field1 = 'O'
AND ....
Then you can do this:
SELECT *
FROM ConditionalSelect
WHERE field1 = 'S'
NOTE: Both SELECT MUST have the same columns, columns types and column names, either the VIEW won't compile.
You could just do something like this:
DECLARE #Field1 VARCHAR(10) =
(
SELECT Field1
FROM table1
);
IF(#Field1 = 'S')
BEGIN
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t2.col1 = t1.col1
END
ELSE IF (#Field1 = 'O')
BEGIN
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t2.col1 = t1.col1
END
Saves you from creating a view

UPDATE with isNull

In Microsoft SQL Server,
if the inner select doesn't have a matching criteria, then I need to update Field1 with blank instead of null.
UPDATE Table1
SET Field1=(
SELECT Field2
FROM Table2
WHERE Table2.Field3 = Table1.Field4
)
Please try like this -
UPDATE a
SET a.Field1 = ISNULL(b.Field2,'')
FROM Table1 a
LEFT JOIN Table2 b ON b.Field3 = a.Field4
You can reference another table using SQL Server's UPDATE ... FROM ... syntax:
UPDATE t1
SET field1 = COALESCE(t2.field2, '')
FROM Table1 t1
LEFT JOIN
Table2 t2
ON t2.Field3 = t1.Field4
The COALESCE() function returns the first non-null expression in a list. You can keep most of your original query, by using COALESCE() to replace any NULL from that query with an empty string.
UPDATE Table1
SET Field1=COALESCE((
SELECT Field2
FROM Table2
WHERE Table2.Field3 = Table1.Field4
), '')
Try this
UPDATE T1
SET T1.Field1=isnull(T2.Field2,'')
from Table1 T1 left join Table2 T2
ON T2.Field3 = T1.Field4

specifying count in WHERE clause

select *
from table1 t1,
table2 t2,
table3 t3
where t2.parent_id = t1.row_id
and t2.xyz is not null
and (
select count(*)
from table3
where xyz = t2.row_id
) = 0;
Will it work?
I am using the alias t2 within my subquery.
My requirement is to check is to specify condition in where clause such that there is no record present in table3 where column xyz of table3 is stored as row_id of table2.
You can use NOT EXISTS to assert that there is no row returned from the subquery. Use modern explicit join syntax instead of comma based legacy syntax. No need to join table3 outside (you were making a cross join effectively).
select *
from table1 t1
join table2 t2 on t2.parent_id = t1.row_id
where t2.xyz is not null
and not exists (
select 1
from table3
where xyz = t2.row_id
);

inner join two SELECT statements in ONE query AND show results from BOTH tables

I want the use 2 SELECT statements and an INNER JOIN from different queries BUT also want to show the two different results, from different tables in the same query. Like this..
SELECT column1 FROM earth e1 that is null
+
SELECT chair5 FROM space s1 that is not null
INNER JOIN space s1 ON e1.car = s1.truck
ORDER BY e1.column,s1.chair5
How do I show the results of two different queries while using an INNER JOIN?
Assume the table T1 contains values 'A','B','C' and table T2 contains values 'A','B','D'
You may query
select 'T1' as source, col from t1
union all
select 'T2' as source, col from t2
union all
select 'join T1,T2' as source, t1.col from t1 inner join t2
on t1.col= t2.col
order by 1,2
;
getting
SOURCE COL
---------- -----
T1 A
T1 B
T1 C
T2 A
T2 B
T2 D
join T1,T2 A
join T1,T2 B
The first column identifies the source: single query or a join
Alternatively, which I'll prefer you may get the same information (more compressed) using FULL OUTER JOIN
with fj as (
select t1.col t1_col, t2.col t2_col
from t1 full outer join t2
on t1.col= t2.col
)
select
case when t1_col is not null and t2_col is not null then 'both'
when t1_col is not null then 'T1 only'
when t2_col is not null then 'T2 only' end as source,
nvl(t1_col, t2_col) col
from fj
order by 1,2
.
SOURCE COL
------- ----------
T1 only C
T2 only D
both A
both B

How to replace IN () with Conditional inner join?

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