Update table column based on another table - sql

I have a table Table1 that look something like this:
Number Name Exist
-------------------------
245435 John
64443 Sam
57133 *NULL*
89373 Jame
and another table Table2:
Number Name
----------------
245435 John
64443 Sam
*NULL* Jame
I'm just wondering how to update the Table1's Exist column with "YES" OR "NO", depending on whether either Table1.Number or Table1.Name or both also exist in Table2. So one value need to be found in Table2.
I have this so far but not sure how to add YES or NO.
UPDATE table1
SET Exist = (SELECT TOP 1 Number
FROM table2
WHERE table1.Number = table2.Number
OR table1.Name = table2.Name);

You just need to add a CASE expression
UPDATE table1
SET Exist = (CASE
WHEN EXISTS (SELECT 1
FROM table2
WHERE table1.Number = table2.Number
OR table1.Name = table2.Name)
THEN 'YES'
ELSE 'NO'
END);

Related

Remove Duplicate Data Based on Three Fields and Two tables

I have two tables that have more than three fields each. There is a group of records that are on both files, the below is a mock example:
Table 1:
ID Name Town State
1 Dave Chicago IL
2 Mark Tea MD
Table 2:
ID Name State Job Married
1 Dave IL Manager Yes
2 Mark MD Driver No
For my purpose duplicates exist if ID, Name, and State are the same. So the above data are duplicates. How do I delete them from one table (I have over 900 duplicates so deleting one by one is not possible)?
delete table1
where ID in(select distinct ID from table1 where ID in (Select ID from table2))
i dont understand which table has duplicates, if you want to delete duplicate data from one table1 then you can use this query
This query will produce a de-duplicated result set:
SELECT Table1.ID,
Table1.NAME,
Table1.Town,
Table1.STATE,
NULL AS Job,
NULL AS Married
FROM Table1
WHERE Table1.ID NOT IN (
SELECT Table1.ID
FROM Table1
INNER JOIN Table2 ON (Table2.STATE = Table1.STATE)
AND (Table2.NAME = Table1.NAME)
AND (Table1.ID = Table2.ID)
)
UNION
SELECT Table2.ID,
Table2.NAME,
NULL AS Town,
Table2.STATE,
Table2.Job,
Table2.Married
FROM Table2
This is the most straightforward way to do it, assuming that you want to delete from Table1. I'm a little rusty with Access SQL syntax, but I believe this works:
DELETE FROM [Table1]
WHERE EXISTS (
SELECT 1
FROM [Table2]
WHERE [Table2].[ID] = [Table1].[ID]
AND [Table2].[Name] = [Table1].[Name]
AND [Table2].[State] = [Table1].[State]
)

SQL: checking if row contains contents of another cell

I'm having difficulty with a SQL query.
I have two tables:
Table 1
ID/First Name
1 Ben
2 Barry
3 Birl
Table 2
ID/Full name
1 Ben Rurth
2 Barry Bird
3 Burney Saf
I want to run a check between the two tables where if the contents of the First Name in Table 1 is not in the Full name in table 2 the result will be returned, e.g. returning id 3, Birl, in the above example.
I have been trying queries like:
SELECT First_Name
from Table_1
WHERE NOT EXIST (SELECT Full_name from Table_2)
with no luck so far.
You can make use of LIKE clause combined with concatenation.
SELECT t1.First_Name,t2.Full_Name
FROM Table1 t1
JOIN Table2 t2 ON t1.ID = t2.ID
WHERE t2.Full_Name NOT LIKE '%' || t1.First_Name || '%'
Or
SELECT t1.First_Name,t2.Full_Name
FROM Table1 t1
JOIN Table2 t2 ON t1.ID = t2.ID
WHERE t2.Full_Name NOT LIKE CONCAT('%', t1.First_Name, '%')
This is, understanding that both tables shares the ID column.

How to check both table having same row values Even Rows are mismatch?

I have Two table consider tbl1 and tbl2
tbl1
Site
---------
101 - Hold
102 - test
tbl2
Site
---------
101 - Hold
104 - wel
102 - test
I want to check tbl2 data is present in tbl1 . If yes select statement has to return 'Yes' else 'No' ...
EXample
If tbl2 is not having "102-test" then also i have to return 'No'
How can i achieve this.
You do this by looking for a non-match and using this information:
select (case when count(*) = 0 then 'Yes' else 'No' end)
from tbl2 t2
where not exists (select 1 from tbl1 where t1.site = t2.site);
The count(*) guarantees that exactly one row is returned, containing yes or no.
SELECT
CASE EXISTS( SELECT Id FROM tbl2 WHERE Id IN (SELECT Id FROM tbl1))
THEN 'Yes'
ELSE 'No'
END
Checking if tbl2 data exists in tbl1 and returning back the site with value either Yes or No when tbl2 exists in tbl1 or not.
No need to use any aggregate and you have information about all tbl1 site's - which is what you wanted to achieve (according to your question).
SELECT
a.site, CASE WHEN b.site IS NOT NULL THEN 'Yes' ELSE 'No END AS value_exists
FROM
tbl1 a
LEFT JOIN tbl2 b ON
a.site = b.site
If you have duplicate values in tbl1 then you might want to consider adding DISTINCT or GROUP BY clause.

Update SQL statement only if two values match in another table

I have been searching for this scenario that has come across my desk, I have been searching reference sites but haven't had luck creating the correct SQL statement to complete this task.
Here is the PSEUDO code for the scenario.
UPDATE TABLE1
SET TABLE1.ID = TABLE1.From_ID,
TABLE1.VALUE = 'ALL'
WHERE TABLE1.From_ID = TABLE2.ID
AND TABLE2.NAME = 'TEST'
Basically I need to update two columns in TABLE1 only if the id from TABLE1 matches the ID's in the TABLE2 and the description column in TABLE2 equals to a string value the caveat is that TABL1 columns can't be change only if there is a correlation between the ID's from TABLE1 and TABLE2 and in TABLE2 that ID correlates to description column for a specific string value. Below is table structure and end result I'm trying to get too.
TABLE1:
FIELD_ID CONDITIONAL_VALUE FROM_FIELDID
--------------------------------------------
1 TEST 3
7 TEST 4
5 ANY 7
TABLE2:
FIELD_ID Description
----------------------------------------------
3 BLUE
4 BLUE
7 RED
In Transact-SQL (SQL Server's dialect of SQL), you need a FROM clause in your SQL if you specify more than the table you're trying to update.
update
TABLE1.ID
set
TABLE1.ID = TABLE1.From_ID ,
TABLE1.VALUE = 'ALL'
from
TABLE1,
TABLE2
where
TABLE1.From_ID = TABLE2.ID
AND TABLE2.NAME = ''TEST
You need to join data from TABLE1 to TABLE2
UPDATE t1
SET t1.ID = t1.From_ID
,t1.VALUE = 'ALL'
FROM Table1 AS t1
JOIN table2 AS t2
ON t1.From_ID = t2.ID
AND t2.NAME = 'TEST't1

Query three non-bisecting sets of data

I am retrieving three different sets of data (or what should be "unique" rows). In total, I expect 3 different unique sets of rows because I have to complete different operations on each set of data. I am, however, retrieving more rows than there are in total in the table, meaning that I must be retrieving duplicate rows somewhere. Here is an example of my three sets of queries:
SELECT DISTINCT t1.*
FROM table1 t1
INNER JOIN table2 t2
ON t2.ID = t1.ID
AND t2.NAME = t1.NAME
AND t2.ADDRESS <> t1.ADDRESS
SELECT DISTINCT t1.*
FROM table1 t1
INNER JOIN table2 t2
ON t2.ID = t1.ID
AND t2.NAME <> t1.NAME
AND t2.ADDRESS <> t1.ADDRESS
SELECT DISTINCT t1.*
FROM table1 t1
INNER JOIN table2 t2
ON t2.ID <> t1.ID
AND t2.NAME = t1.NAME
AND t2.ADDRESS <> t1.ADDRESS
As you can see, I am selecting (in order of queries)
Set of data where the id AND name match
Set of data where the id matches but the name does NOT
Set of data where the id does not match but name DOES
I am retrieving MORE rows than exist in T1 when adding up the number of results returned from all three queries which I don't think is logically possible, plus this means I must be duplicating rows (if it is logically possible) somewhere which prevents me from executing different commands against each set (since a row would have another command executed on it).
Can someone find where I'm going wrong here?
Consider if Name is not unique. If you have the following data:
Table 1 Table 2
ID Name Address ID Name Address
0 Jim Smith 1111 A St 0 Jim Smith 2222 A St
1 Jim Smith 2222 B St 1 Jim Smith 3333 C St
Then Query 1 gives you:
0 Jim Smith 1111 A St
1 Jim Smith 2222 B St
Because rows 1 & 2 in Table 1 match rows 1 & 2, respectively in Table 2.
Query 2 gives you nothing.
Query 3 gives you
0 Jim Smith 1111 A St
1 Jim Smith 2222 B St
Because row 1 in Table 1 matches row 2 in Table 2 and row 2 in Table 1 matches row 1 in Table 2. Thus you get 4 rows out of Table 1 when there are only 2 rows in it.
Are you sure that NAME and ID are unique in both tables?
If not, you could have a situation, for example, where table 1 has this:
NAME: Fred
ID: 1
and table2 has this:
NAME: Fred
ID: 1
NAME: Fred
ID: 2
In this case, the record in table1 will be returned by two of your queries: ID and NAME both match, and NAME matches but ID doesn't.
You might be able to narrow down the problem by intersecting each combination of two queries to find out what the duplicates are, e.g.:
SELECT DISTINCT t1.*
FROM table1 t1
INNER JOIN table2 t2
ON t2.ID = t1.ID
AND t2.NAME = t1.NAME
AND t2.ADDRESS <> t1.ADDRESS
INTERSECT
SELECT DISTINCT t1.*
FROM table1 t1
INNER JOIN table2 t2
ON t2.ID = t1.ID
AND t2.NAME <> t1.NAME
AND t2.ADDRESS <> t1.ADDRESS
Assuming that T2.ID has a unique constraint, it is still quite logically possible for this scenario to occur.If for every record in T1, there are two corresponding records in T2:
Same name, same id, different address
Same name, different id, different address
Then the same record for T1 can come up in the first and third query for example.
It is also possible to simultaneously get the same row in the second and third query.
If T2.ID is not guaranteed to be unique, then you could get the same row from T1 in all three queries.
I think the last query could be the one fetching extra set of rows.
i.e. It is relying on Name matching in both tables (and not on ID)
To find the offending data (and help find your logic hole) I would recommend:
(caution pseudo-code)
Limit the results to just SELECT id FROM ....
UNION the result sets
COUNT(id)
GROUP BY id
HAVING count(id) > 1
This will show the records that match more than one sub-query.