I have two tables
Table A
Number
111
222
333
444
Table B
Number Another
111 AAA
222 BBB
666 CCC
777 DDD
What I am would like to do, is apply an UPDATE statement conditional on whether the "Number" value in Table B exist in Table A. So the table would end up looking something like this.
Number Another
111 ZZZ
222 ZZZ
666 CCC
777 DDD
I know I need to use an UPDATE query and probably some kind of JOIN, but I am not sure on the syntax.
Any help greatly appreciated.
Yes. You need to update using a join like this:
update t2
set t2.Another = 'ZZZ'
from table1 t1
join table2 t2 on t1.Number = t2.Number
You can use exists also.
Query
update t1
set t1.[Another] = 'ZZZ'
from [TableB] t1
where exists(
select 1 from [TableA] t2
where t1.[Number] = t2.[Number]
);
You can use directly SELECT FROM table1 and update into table2 :
UPDATE table2 SET Another = 'ZZZ'
FROM table1 t1 WHERE t1.Number = table2.Number
Related
I have two tables, Table1 and Table2 which have in common two fields EmployeeId and ProjectId
Table1
EmployeeId
ProjectId
1
111
2
222
3
333
4
444
Table2
ProjectId
EmployeeId
222
5
111
1
444
4
333
2
555
8
I want to write a query that creates a column called 'Match' indicating whether an EmployeeId/ProjectId combo that appears in Table1 also appears in Table2. For example the first and fourth rows of Table1 should be indicated as matches since those combos appear in Table2 The final output should look like the following :
EmployeeId
ProjectId
Match
1
111
yes
2
222
no
3
333
no
4
444
yes
If anyone knows how to write this sort of query, I would really appreciate the help.
You need a left join on the field of employeeId and ProjectId.
So you will use table1 as the left table, and join table2 as the right table. Then where table2 fails to join, you will get NULL and you can write NO for the match...
SELECT
t1.employeeId,
t1.projectId,
CASE
WHEN t2.employeeId IS NULL THEN 'no' ELSE 'yes'
END AS match
FROM
table1 AS t1
LEFT JOIN
table2 AS t2
ON t2.employeeId = t1.employeeId
AND t2.projectId = t2.projectId
The left table, table1, will serve as the 'permanent' data that populates in the results-set, ie. you will get the whole table (t1) in the output.
The right table, table2, will only join, when there is a condition that employeeId matches AND projectId matches.
Because we choose a left join, the records from t1 that don't have a match in t2 will result in NULL from t2.
I try co create a SQL query with two inner joins and an if case. I create an example to explain what I mean:
ID
Typ
Case
123
AAA
zzz
124
BBB
yyy
125
CCC
yyy
Typ1
ID1
AAA
888
BBB
999
CCC
777
ID2
Result
666
1
555
2
777
3
In words, the query should do:
Search in the first table for ID 125, so I get Typ CCC and Case yyy
If case is yyy then search in the second table for CCC in column Typ1, here I get the ID 777 and then search in the third table for 777 in column ID2 to get the result 3.
If case is not yyy then just show me the results of the first table.
The result should be:
ID
Typ
Result
123
AAA
No match
124
BBB
No match
125
CCC
3
I hope you can understand what I try to explain :)
You want to select data from the first table and only show data from the other tables where appropriate. So, outer join the other tables.
select t1.id, t1.typ, t3.result
from t1
left outer join t2 on t2.typ1 = t1.typ and t1.case = 'yyy'
left outer join t3 on t3.id2 = t2.id1
order by t1.id;
I am new to SQL and I want to do an UPSERT into a table with select from another table. For example I have 2 tables
TABLE1
ID DATE VALUE
1 23.09.2020 abc
2 01.02.2020 def
TABLE2
ID VALUE ADDRESS
1 xyz mmm
2 zzz nnn
2 zzz ppp
3 ccc qqq
The task is - If the ID in TABLE1 is of DATE = '23.09.2020' and ID is present in TABLE2 then update VALUE column in TABLE1 with the VALUE in TABLE2. And if ID in TABLE1 is present in TABLE2 but does not have DATE as '23.09.2020' then insert distinct (ID,VALUE) from TABLE2 into TABLE1. And if ID in TABLE1 is not present in TABLE2 then do nothing. So final result in TABLE1 after UPSERT should look like.
TABLE1
ID DATE VALUE
1 23.09.2020 xyz
2 01.02.2020 def
2 23.09.2020 zzz
NOTE : ID column is not a primary key, and I cannot make it as primary key.
I have tried something like below, but getting error and not able to achieve desired result.
upsert TABLE1(ID,DATE,VALUE)
SELECT DISTINCT ID,'23.09.2020',VALUE FROM TABLE2
WHERE TABLE1.ID = TABLE2.ID AND TABLE1.DATE = '23.09.2020'
UPDATE - I tried using MERGE as suggested, but getting ID = 2 inserted twice in TABLE1, where as I want it inserted only once as distinct (ID,VALUE) from TABLE2. Below is the MERGE query I tried.
MERGE INTO TABLE1 T1
USING TABLE2 T2 ON T1.ID = T2.ID AND T1.DATE = '23.09.2020'
WHEN MATCHED THEN
UPDATE SET T1.VALUE = T2.VALUE
WHEN NOT MATCHED THEN
INSERT(ID,DATE,VALUE) VALUES(T2.ID,'23.09.2020',T2.VALUE);
Result I am getting
TABLE1
ID DATE VALUE
1 23.09.2020 xyz
2 01.02.2020 def
2 23.09.2020 zzz
2 23.09.2020 zzz --> Duplicate, not wanted.
Result I want
ID DATE VALUE
1 23.09.2020 xyz
2 01.02.2020 def
2 23.09.2020 zzz
How can I insert distinct of (ID,VALUE) from TABLE2 using MERGE query ?
Could you try with below,
You have almost done everything right as I see, what I did use a sub query to select the distinct ID and VALUE and then join.
MERGE INTO TABLE1 T1
USING (SELECT DISTINCT ID,VALUE
FROM TABLE2 T2
) T2
ON ( T1.ID = T2.ID
AND T1.DATE = '23.09.2020')
WHEN MATCHED
THEN
UPDATE SET T1.VALUE = T2.VALUE
WHEN NOT MATCHED
THEN
INSERT(ID,DATE,VALUE)
VALUES(T2.ID,'23.09.2020',T2.VALUE);
I have:
TABLE1 with ID, and Name.
TABLE2 with ID, and Address.
I want to get ALL TABLE1 records and add a STATUS column:
If this record exists in TABLE2 - 'OK'.
If this record is not exist in TABLE2, then research a match by last 2 digits, else 'NO_RECORD'.
If this record has duplicate records in TABLE2, then if the duplicate records has the same Address choose one record - 'OK', and if they doesn't has the same Address - 'DUPLICATE'.
Meanwhile, I've started with this:
SELECT t1.id,
t1.name,
t2.Address,
iif(Address is null, 'No_RECORD', 'Ok') as 'status'
FROM Table1 as t1
left join Table2 as t2 on t1.id = t2.id
For example:
Table1
id Name
111 aaa
222 bbb
333 ccc
444 ddd
555 eee
666 fff
777 ggg
888 hhh
999 iii
Table2:
id Address
111 rr
922 hfh
444 vbv
444 vbv
555 xxa
555 plo
555 plo
666 wqq
777 gyt
999 ree
999 ree
My accepted results are:
id name Address 'status'
111 aaa rr Ok
222 bbb hfh Ok
333 ccc No_RECORD
444 ddd vbv Ok
555 eee Duplicate
666 fff fff Ok
777 ggg wqq Ok
888 hhh No_RECORD
999 iii ree Ok
444 is not duplicate because the 2 table2's records address are match,
555 is duplicate because the 3 table2's records address are mismatch,
999 is not duplicate because the 2 table2's records address are match.
222 is ok because tha 2 last digits of it's id are exist in tabl2: "922".
How can I continue? (I use sql query in access).
I have no idea what "match by last five digits" is supposed to mean. It has nothing to do with your sample data, so I'm just ignoring that part of the question.
What you want to do is to aggregate on table2 before doing the join:
select t1.id, t1.name,
iif(multiple_addresses = 0, address, null) as address,
switch(t2.id is null, "No_Record",
multiple_addresses = 1, "Duplicate",
1=1, "OK"
) as status
from table1 as t1 left join
(select id, min(address) as address
iif(min(address) = max(address), min(address), 0, 1) as multiple_addresses
from table2
group by id
) as t2
on t1.id = t2.id;
You can use a case statement to get this status:
SELECT
t1.id,
t1.name,
t2.Address as add2,
case
when t2.Address is null and exists (select 1 from Table2 temp where temp.id = t1.id) then 'DUPLICATE'
when t2.Address is null then 'NO_RECORD'
else 'OK' end
as 'status'
FROM Table1 as t1
left join Table2 as t2 on t1.id = t2.id and t1.name = t2.Address
order by t1.id
As you can see I added and t1.name = t2.Address to the join clause to make sure you have non null Table2 values only when you want so.
About the case when, the first condition checks if 1) no corresponding record was found 2) there are records for the same id, meaning 'DUPLICATE'. The second condition checks that there are no corresponding record, and we already know there are no duplicates (as it would have fallen into the first case).
Working SQLFiddle.
In a database there are two tables.
Table1 and Table2
Table1:
column1 column2 column3
271 211 111
301 333 333
Table2:
ColumnNo Value Desc
1 271 aaa
3 111 bbb
2 211 ccc
2 333 ddd
1 301 eee
Here columnNo refers to the 1st table>> 1=Column1,2=column2,3=Column3.
so i have to update the 1st table column value with the Desc.
so the updated table1 will be
column1 column2 column3
aaa ccc bbb
eee ddd ddd
like this.
As there are lots of column and value so how can i change the column name dynamically and update the value?
Try this
UPDATE T1
SET T1.column1 = T2.Desc,T1.column2 = T3.Desc,T1.column3 = T4.Desc
FROM Table1 T1 Inner Join Table2 T2 ON T1.column1 = T2.Value
Inner Join Table2 T3 ON T1.column2 = T3.Value
Inner Join Table2 T4 ON T1.column3 = T4.Value
FIDDLE DEMO