How to select Boolean value from sub query with IF EXISTS statement (SQL Server)?
It should be something like :
SELECT
TABLE1.Id,
NewFiled = (IF EXISTS(SELECT Id FROM TABLE2 WHERE TABLE2.ID = TABLE1.ID)
SELECT 'TRUE'
ELSE
SELECT 'FALSE')
FROM TABLE1
Use CASE:
SELECT
TABLE1.Id,
CASE WHEN EXISTS (SELECT Id FROM TABLE2 WHERE TABLE2.ID = TABLE1.ID)
THEN 'TRUE'
ELSE 'FALSE'
END AS NewFiled
FROM TABLE1
If TABLE2.ID is Unique or a Primary Key, you could also use this:
SELECT
TABLE1.Id,
CASE WHEN TABLE2.ID IS NOT NULL
THEN 'TRUE'
ELSE 'FALSE'
END AS NewFiled
FROM TABLE1
LEFT JOIN Table2
ON TABLE2.ID = TABLE1.ID
You can also use ISNULL and a select statement to get this result
SELECT
Table1.ID,
ISNULL((SELECT 'TRUE' FROM TABLE2 WHERE TABLE2.ID = TABEL1.ID),'FALSE') AS columName,
etc
FROM TABLE1
SELECT Id, 'TRUE' AS NewFiled FROM TABEL1
INTERSECT
SELECT Id, 'TRUE' AS NewFiled FROM TABEL2
UNION
SELECT Id, 'FALSE' AS NewFiled FROM TABEL1
EXCEPT
SELECT Id, 'FALSE' AS NewFiled FROM TABEL2;
Use a CASE statement and do it like this:
SELECT
T1.Id [Id]
,CASE WHEN T2.Id IS NOT NULL THEN 'TRUE' ELSE 'FALSE' END [Has Foreign Key in T2]
FROM
TABLE1 [T1]
LEFT OUTER JOIN
TABLE2 [T2]
ON
T2.Id = T1.Id
You can use EXISTS to check if a column value exists in a different table.
SELECT
TABLE1.id,
EXISTS (SELECT 1 FROM TABLE2 WHERE TABLE2.id = TABLE1.id) AS columnName
FROM TABLE1
Example:
CREATE TABLE TABLE1 (
id INTEGER PRIMARY KEY,
some_column TEXT NOT NULL
);
CREATE TABLE TABLE2 (
id INTEGER PRIMARY KEY,
some_column TEXT NOT NULL
);
INSERT INTO TABLE1 VALUES
(111, 'lorem ipsum'),
(222, 'and'),
(333, 'some'),
(444, 'random'),
(123, 'strings');
INSERT INTO TABLE2 VALUES
(111, 'lorem ipsum'),
(444, 'random'),
(123, 'strings');
SELECT
TABLE1.id,
EXISTS (SELECT 1 FROM TABLE2 WHERE TABLE2.id = TABLE1.id) AS columnName
FROM TABLE1
Output:
id
someColumn
111
1
123
1
222
0
333
0
444
1
Related
I am currently trying to obtain data from two tables that have the same columns. The values for primary key "ID" of both tables may exist in one or both tables. Even with same primary keys, the values in different columns may not be the same for both tables. My question is given I have an ID testID, how do I query where in I first check table1 if it exists. If it exists in table1 I use the details found in table1, otherwise check table2 and use details in table2 if it exists in table2.
Either use a FULL OUTER JOIN:
select
case when t1.id is not null then t1.field1 else t2.field1 end as field1,
case when t1.id is not null then t1.field2 else t2.field2 end as field2,
...
from table1 t1
full outer join table2 t2 on t2.id = t1.id
where t1.id = :testid or t2.id = :testid;
Or UNION ALL in combination with NOT EXISTS:
select field1, field2, ...
from table1
where id = :testid
union all
select field1, field2, ...
from table2
where id = :testid and not exists (select * from table1 where id = :testid);
The possible way is to use FULL OUTER JOIN
SELECT t1.id,
t2.id,
CASE
WHEN t1.id IS NOT NULL
AND t2.id IS NOT NULL
THEN 'ID in both sources'
WHEN t1.id IS NULL
THEN 'ID in T2 only'
WHEN t2.id IS NULL
THEN 'ID in T1 only'
END source_key
FROM t1
FULL OUTER JOIN t2
ON t1.id = t2.id
WHERE t1.id = 1 -- your test_id here
OR t2.id = 1; -- your test_id here
Cheching if T1.ID/ T2.ID is not NULL you get the information if the record is defined in the respective source table.
Select FieldA, " if data exists in table two return true else false "
from Table1
Left join Table2 on Table1.Id=Table2.Id
This is how I currently do the requirement above:
Select FieldA, (case when Table2.Table2Id is not null then 1 else 0 End)
from Table1
Left join Table2 on Table1.Id=Table2.Id
Is there any replacement for the "Case" statement, to something like ifExists(Table2.Id) ?
You can use EXISTS, i guess you want the strings True/False, otherwise cast 1/0 to bit:
SELECT Id,
DataExists = CASE WHEN EXISTS
(
SELECT 1 FROM Table2 WHERE Table2.Id = Table1.Id
) THEN 'True' ELSE 'False' END
FROM dbo.Table1
For the sake of completeness:
SELECT Id,
DataExists = CAST((CASE WHEN EXISTS
(
SELECT 1 FROM Table2 WHERE Table2.Id = Table1.Id
) THEN 1 ELSE 0 END) AS BIT)
FROM dbo.Table1
May be you can use isNull as well.
Select FieldA, isnull((SELECT 'TRUE' FROM TABLE2 WHERE TABLE2.ID = TABEL1.ID),'FALSE') AS columName
from Table1
What I am trying to do can be exemplified (Incorrectly) by the following query
SELECT * FROM Table1
WHERE User IN
(CASE (SELECT Username From Table2 Where Table2.ID = 1)
WHEN IS NULL THEN NULL
ELSE (SELECT Username From Table2 Where Table2.ID = 1) END)
Since User = NULL is not the same as User IS NULL something with the above syntax won't work. Is there a way to do this? I don't want to grab NULLS if there are records in Table2.
For Example,
Table1
ID User
1 Elias
2 NULL
Table2
ID Username
1 Elias
2 NULL
I would want the above select to return the following recordset
ID User
1 Elias
If I was looking for ID 2 in Table 2 I would want the following recordset
ID User
2 NULL
If you want to consider two NULL values as matching, you can do:
select t1.*
from table1 t1
where exists (select 1
from table2 t2
where t1.user = t2.username or t1.user is null and t2.user is null
);
If you are trying to match values in table2 and if there are no values in table2 then return values equal to NULL (how I interpret the title):
select t1.*
from table1 t1
where exists (select 1
from table2 t2
where t1.user = t2.username or t1.user is null and t2.user is null
) or
(not exists (select 1 from table2) and t1.user is null);
EDIT:
For performance, you can do:
select t1.*
from table1 t1
where exists (select 1
from table2 t2
where t1.user = t2.username
) or
exists (select 1
from table2
where t1.user is null and t2.user is null
) or
(not exists (select 1 from table2) and t1.user is null);
These can take advantage of an index on table2(user).
A link where you use null as a relation between table will get you a wrong number of rows. Duplicated rows and stuff.
The simplest query I can think of (in order to avoid this) is:
select t1.*
from table1 t1
join table2 t2
on t1.user = t2.username
This will filter out the nulls.
In case that you also need to aggregate here null use Gordon's query, though if you have multiple nulls in one of the tables then your query is meaningless.
If u want to use IN clause
SELECT * FROM Table1
WHERE User IN
(SELECT isnull(Username,'') From Table2 Where 1 = 1)
Table 1
Column1 Column2 Column 3
A Null 12
B Null 15
C 0 15
Table 2
Column2 Column3
0 15
0 12
I have table 1 and Table 2 , Here I'm passing table 2 parameters to table 1 which should return the column1 but it should match with Null values like the below scenario
If I pass (0, 15) to table 1 then it should return 'C' not 'B'.
If I Pass (0,12) to table 1 then it should return 'A'
Anytime it should return one value not multiple vales.
Could you please help me with this logic ?
You could do this as a union:
select Column1 from
(
select Table1.Column1 as Column1,Table1.Column2 as Column2
from Table1
join Table2 on
Table1.Column2=Table2.Column2 and Table1.Column3=Table2.Column3
UNION
select Table1.Column1 as Column1,Table1.Column2 as Column2
from Table1
join Table2 on
Table1.Column2 is NULL and Table1.Column3=Table2.Column3
) as Unioned
ORDER BY Column2 NULLS LAST
LIMIT 1;
The UNION discards duplicates, but you could always wrap the entire statement in another SELECT if you explicitly need to tell it to return at most one value.
select *
from table2 t
left outer join table1 t1 on t.column2=t1.column2 and t.column3=t1.column3
left outer join table1 t2 on t2.column2 is null and t.column3=t2.column3
where t1.column2 is not null or t2.column3 is not null
SQL Server
WITH T1
as
(SELECT COLUMN1,COLUMN2,
case when COLUMN2 is null then 0 else COLUMN2 end AS TRANSFORMED_COLUMN2,
row_number() OVER (partition by isnull(column3,2) ORDER BY isnull(COLUMN2,2) asc) AS rnk,
COLUMN3
FROM TABLE1)
SELECT
T1.COLUMN1,
T2.COLUMN2,
T2.COLUMN3
FROM
T1
INNER JOIN
(SELECT COLUMN2,COLUMN3 FROM TABLE2) T2
ON T1.TRANSFORMED_COLUMN2 = T2.COLUMN2
AND T1.COLUMN3=T2.COLUMN3
where T1.rnk=1
By using CTE, you will running through the tables just once.
SELECT t1.* FROM table1 t1
INNER JOIN table2 t2
ON NVL(t1.column2,0) = t2.column2 and t1.column3 = t2.column3
Is there a quicker way to get the ids that exist in table1 but not exist in table2 and insert them in table2?
insert into table2 (id)
select id
from table1
where table1.id not in (select id from table2)
In addition to your solution using the in operator try the exists one
select id
from table1 t1
where not exists (
select 1
from table2
where id = t1.id
)
If the subquery returns an empty set not exists evaluates to true
The outer join
select id
from
table1 t1
left join
table2 t2 on t1.id = t2.id
where t2.id is null
Use explain analyze to compare