Boolean - Does ID Exist in Table? - sql

I have two tables... A master ID table and a results ID table with only a few IDs from the master table. I'm looking to create the following SQL Query:
Select
A.ID
(Case when B.ID is in A.ID 1 Else 0 End) as is_found
From
master_table as A
LEFT JOIN results_table as B
ON A.ID = B.ID
The resulting table should have all IDs from master table with a boolean column saying if the ID was found in the results table. Thank you for your help!!

I would use case . . . exists:
Select mt.id,
(case when exists (select 1 from results_table rt where rt.id = mt.id) then 1 else 0 end) as is_found
From master_table ;

First, consider the case where results_table will have either zero or one matching row; in this case, the LEFT JOIN will always give one row for each ID, and B.ID will be NULL if there is no corresponding row in results_table.
We can therefore use a simple CASE to test this:
Select
A.ID,
CASE WHEN B.ID IS NOT NULL THEN 1 ELSE 0 END as is_found
From
master_table as A
LEFT JOIN results_table as B
ON A.ID = B.ID
If there may be more than one row in results_table for the same ID, the LEFT JOIN may in turn create several rows, one for each match.
The result of the CASE statement will be the same for all values of A.ID - if there are zero matches, it will occur once with value 0, and if there are one or more, it will always have the value 1. So we can simply take distinct values of the entire query:
Select Distinct
A.ID,
CASE WHEN B.ID IS NOT NULL THEN 1 ELSE 0 END as is_found
From
master_table as A
LEFT JOIN results_table as B
ON A.ID = B.ID

Related

SQL inner join with conditional selection

I am new in SQL. Lets say I have 2 tables one is table_A and the other one is table_B. And I want to create a view with two of them which is view_1.
table_A:
id
foo
1
d
2
e
null
f
table_B
id
name
1
a
2
b
3
c
and when I use this query :
SELECT DISTINCT table_A.id, table_B.name
FROM table_A
INNER JOIN table_B ON table_B.id = table_A.id
the null value in table_A can't be seen in the view_1 since it is not found in table_B. I want view_1 to show also this null row like :
id
name
1
a
2
b
null
no entry
Should I create a 4. table? I couldn't find a way.
Try this Query:
SELECT DISTINCT a.id,(CASE When b.name IS NULL OR b.name = '' Then 'No Entry' else b.name end) name FROM table_A a
LEFT JOIN table_B b on a.id = b.id
You are looking for an outer join. Thus you keep all table_A rows and join table_B rows where they exist. If no match exists, the table_B columns in the joined row are NULL.
You replace NULLs with a value with COALESCE.
SELECT a.id, COALESCE(b.name, 'no entry') AS name
FROM table_a a
LEFT OUTER JOIN table_b b ON b.id = a.id
ORDER BY a.id NULLS LAST;
You haven't tagged your request with your DBMS. Not all DBMS support the NULLS LAST clause.
Please note that there is no DISTINCT in my query. It is not needed. And every time you think you must use DISTINCT, think twice. SELECT DISTINCT is very seldom needed. Most often it is used, because the query is kind of flawed and causes the undesired duplicates itself.

Returning 1 or 0 instead of value or null in T-SQL

In my SELECT statement, I join two tables. It's possible that there's no corresponding value in Table2 in which case, my SELECT statement should return NULL for that column. What I want to do is that instead of returning the actual value coming from Table2 I want to return a 1.
I thought I could use ISNULL for this but that function is designed to return the actual value if one exists.
This is what my SELECT statement looks like:
SELECT a.Id, ISNULL(b.PersonId, 0)
FROM Table1 AS a
LEFT OUTER JOIN Table2 AS b ON a.Id = b.Id
Here the PersonId column is of UNIQUEIDENTIFIER type and I don't want to return either a Guid or a 0. Instead, I'd like to return a 1 or a 0.
How can I handle this scenario?
I think you're looking for a CASE EXPRESSION here.
SELECT a.Id, CASE WHEN b.PersonId IS NOT NULL THEN 1 ELSE 0 END
FROM Table1 AS a
LEFT OUTER JOIN Table2 AS b ON a.Id = b.Id
As well as #DaleK's excellent answer, another option is using EXISTS
SELECT
a.Id,
CASE WHEN EXISTS (SELECT 1
FROM Table2 AS b
WHERE a.Id = b.Id)
THEN 1 ELSE 0 END
FROM Table1 AS a
Note that the semantic is different here, as it is a semi-join: the rows are not duplicated if there are multiple matches in Table2

How to do "case when exists..." in spark sql

What I am trying to do is
case when exists (select 1 from table B where A.id = B.id and B.value in (1,2,3)) then 'Y' else 'N' end as Col_1
It seems like "left semi join" can take care of multiple matching issue, but my understanding is that "left semi join" does not allow using columns from the right (B) table, so how can I add condition "B.value in (1,2,3)"?
The normal way to do this is to left outer join to a summary of table b:
Select a.id, Case When IsNull(b.id) Then 'N' else 'Y' end as Col_1
From A Left Outer Join
(Select distinct id from tableb) b On A.id=b.id
That way you are not repeatedly executing a lookup query for every id in A.
Addition
Your comment indicated that you are trying to create multiple Y/N columns based on b values. Your example had a Y/N for col1 when there was a 1,2,3 and a Y/N for col2 when there was a 4,5,6.
You can get there easily with one summarization of table b :
Select a.id, Case When IsNull(b.val123) Then 'N' else 'Y' end as Col_1,
Case When IsNull(b.val456) Then 'N' Else 'Y' end as Col_2
From A Left Outer Join
(Select id, max(Case When value in (1,2,3) Then 'Y' End) as val123
max(Case When value in (4,5,6) Then 'Y' End) as val456
From tableb
Group By id) b On A.id=b.id
This still accomplishes that lookup with only one summarization of table b.

Update column in Oracle table with value from another table with duplicates

I am trying to update the column (REPT_IND) from table A to the value in table B where A.ID = B.ID and some conditions in table B.
There are some duplicates in table B, but nonetheless the REPT_IND is the same and I still need the value.
How can I do this on Oracle? Any tips are appreciated thank you!
The Following code has the Error:
ORA-01427: single-row subquery returns more than one row
Code:
UPDATE A
SET REPT_IND= (
SELECT B.REPT_IND
FROM B
INNER JOIN A
ON B.ID = A.ID
where A.ID = B.ID
and B.job_type = 'P'
and B.FT_PT is not null
);
You can try also merge statement:
merge into a
using (
select a.id,max(b.rept_ind) rept_ind
from a left join b on a.id=b.id
where b.job_type = 'p'
and b.ft_pt is not null
) b
on (a.id=b.id)
when matched then update
set a.rept_ind=b.rept_ind;
Or if you do not want to set a.rept_ind to null if there is no relevant rows in b:
merge into a
using (
select b.id, max(b.rept_ind) rept_ind
from b
where
b.job_type = 'p'
and b.ft_pt is not null
group by b.id
) b
on (a.id=b.id)
when matched then update
set a.rept_ind=b.rept_ind;
Consider:
update a
set rept_ind= (
select max(b.rept_ind)
from b
where
a.id = b.id
and b.job_type = 'p'
and b.ft_pt is not null
);
There is no need to join table a again in the subquery - a correlation clause is enough. And you can work around possible duplicates by turning on aggregation, which guarantees that only one row will be returned.
You could also use select distinct instead of select max(...) in the subquery. This is somehow more accurate since it does ensure that the multiple rows have the same rept_ind (it they do not, then you would still get the ORA-01427 error).
Just use a correlated subquery . . . and do not repeat the table reference in the subquery:
UPDATE A
SET REPT_IND = (SELECT B.REPT_IND
FROM B
WHERE B.ID = A.ID AND
B.job_type = 'P' AND
B.FT_PT is not null AND
rownum = 1
);

TSQL/SQL 2005/2008 Return Row from one table that is not in othe table

I have to compare a row of one table that is not in another table
TableA
ID
1
2
3
4
NULL
TableB
ID
1
4
When comparing TableA with TableB ,the following o/p (NULL Can be ignored)
ID STATUS
1 FOUND
2 NOT FOUND
3 NOT FOUND
4 FOUND
I tried with
SELECT
case T.ID when isnull(T.ID,0)=0 then 'NOT FOUND'
case T.ID when isnull(T.ID,0)<>0 then 'FOUND'
end,T.ID
FROM
TableA T
LEFT JOIN
TableB N
ON T.ID=N.ID
Its ended with incorrect syntax near '=',moreover i have no idea whether the query is correct.
Try this:
SELECT a.ID,
CASE WHEN b.ID IS NULL THEN 'NOT FOUND' ELSE 'FOUND' END AS Status
FROM TableA a
LEFT JOIN TableB b ON a.ID = b.ID
Note the difference in the structure of the CASE statement - that was your problem.
To generate the result as shown in the question:
SELECT ID,
CASE WHEN EXISTS (SELECT * FROM TableB WHERE ID = TableA.ID)
THEN 'FOUND'
ELSE 'NOT FOUND'
END AS STATUS
FROM TableA
But if you are only interested in the missing records:
SELECT ID
FROM TableA
WHERE NOT EXISTS (SELECT * FROM TableB WHERE ID = TableA.ID)
SELECT
T.ID
FROM TableA T WHERE NOT EXISTS ( SELECT X.ID FROM TableB X WHERE X.ID = T.ID)
If you want the 'Found' or 'Not Found' answer go for what AdaTheDev posted