Removing intermediate table - sql

Given the following tables:
Table1
| id | intermediate_id |
Itermediate
| id |
Table2
| id | intermediate_id | table1_id|
How do i update Table2 with Table1.ids?
I'm trying the following:
update Table2
set table1_id =
(select table1.id
from table1
where table1.intermediate_id = table2.intermediate_id);
which gives me "ERROR: more than one row returned by a subquery used as an expression"

update Table2
set table1_id = table1.id
from table1
where table1.intermediate_id = table2.intermediate_id

Related

Update multiple columns of table based on another table sql

I have two tables:
Table1:
column1|column2|column3|column4|column5
---------------------------------------
'test1'| null | 12 |'old1' | 'gr1'
'test1'| 123 | null |'old2' | 'gr2'
'test2'| 488 | null |'old3' | 'gr3'
'test3'| 488 | null |'old4' | 'gr4'
Table2: (it's a SELECT query)
column1|column2|column4|column5
-------------------------------
'test1'| 999 | 'new1'| 'gr2'
'test3'| 1355 | 'new4'| 'gr4'
I have created the second table as a query in order to update the values of the first, where the key of the first table is (column1, column5). So the table I am after is:
Table1:
column1|column2|column3|column4|column5
---------------------------------------
'test1'| null | 12 |'old1' | 'gr1'
'test1'| 999 | null |'new1' | 'gr2'
'test2'| 488 | null |'old3' | 'gr3'
'test3'| 1355 | null |'new4' | 'gr4'
How is this done?
The exakt SQL command will of course depend on the query which produces your second table. Assuming, this query would just be "SELECT * FROM yourtable2", you can do following update command to achieve your goal:
UPDATE yourtable
SET
column2 = x.column2,
column4 = x.column4
FROM (SELECT * FROM yourtable2) x
WHERE yourtable.column1 = x.column1
AND yourtable.column5 = x.column5
Here you see this is working (in case the table "yourtable2" provides the correct data): db<>fiddle
So, you can replace the "SELECT FROM yourtable2" by your query and it will work.
Can you try
UPDATE table1 t1 set (column2,column4) = (
SELECT column2,column4
FROM table2 t2
WHERE t1.column1 = t2.column1 and t1.column5 = t2.column5
)
If you have table2 as a query, you can use WITH
WITH table2 (column1,column2,column4,column5) AS (
<your select query>
),
UPDATE table1 t1 set (column2,column4) = (
SELECT column2,column4
FROM table2 t2
WHERE t1.column1 = t2.column1 and t1.column5 = t2.column5
)

Update a table based on a condition

I have a column Table1.Tradedate and another column Table2.SettlementDate.
Based on the comparison between these 2, I want to update a column in table 2
IF (Table1.TradeDate <= Table2.SettlementDate)
BEGIN
UPDATE Table2 SET Status='Y'
END
This is what I have tried but I know its wrong, since the table will obviously contain more than 1 records. So, I believe what I should do is
use a join on 2 tables based on some #id to pick a particular record
check the IF condition for that particular record
update the Status column in table2.
I hope my approach is correct but I am writing it incorrectly.
Table1:
SKacc | Name | TradeDate | Othercolumns....
1 | xxx | 01/07/2019 |
2 | xxx | 01/06/2019 |
Table2:
SKAcc | Name | SettlementDate | Status |Other Columns....
1 | xxx | 01/08/2019 | NULL |
2 | xxx | 01/08/2019 | NULL |
Try below
update t2 set Status = 'Y'
from table2 t2
join table1 t1 on t1.id = t2.id
where t1.tradeDate <= t2.settlementDate
Try joining the two tables with the related column and then update the table you want to update with the value. Using inner join in the example but can change depending on the usecase
UPDATE Table2
SET Status = 'Y'
FROM Table2
INNER JOIN Table1 ON Table1.id = Table2.table1_id
WHERE Table1.TradeDate <= Table2.SettlementDate
I would not recommend a JOIN for this purpose. Instead:
update table2
set Status = 'Y'
where exists (select 1
from table1 t1
where t1.id = t2.id and
t1.tradeDate <= t2.settlementDate
);
The reason I recommend this version is because you have not specified that id is unique in table1. In general, you only want to use JOIN in UPDATE when you can guarantee that there is only one matching row.

ORACLE : Update query using two table with has relation

I need to update the column data based on matching record found in both Table.
I want to update the record of NAME column from TABLE2
Following are the tables
Table1
---------------
Id | Name | color
1 | abc | red
2 | def | green
3 | ghi | blue
Table2
---------------
Id | Name | color |fiedId
1 | abc | red | 1
2 | def | green | 1
3 | ghi | blue | 2
Here table1 ID column is the Foreign Key in table2 as fieldId.
So, I want to update all the record which fall under this condition
table1.id = table2.fieldId
Yet another option, using MERGE:
merge into table2 t2
using (select id, name from table1) x
on (t2.fieldid = x.id)
when matched then update set
t2.name = x.name;
Or, for setting the name to 'xxx':
merge into table2 t2
using (select id from table1) x
on (t2.fiedid = x.id)
when matched then update set
t2.name = 'xxx';
Sounds like you just want an update like this:
update table2 t2
set t2.name =
( select t1.name
from table1 t1
where t1.id = t2.fieldid )
Regarding the followup question:
what if i want to set Name = "xxx" for all matching rows?
update table2 t2
set t2.name = 'xxx'
where t2.fieldid in
( select t1.id from table1 t1 )
or this can be written as:
update table2 t2
set t2.name = 'xxx'
where exists
( select null from table1 t1
where t1.id = t2.fieldid )

SQL Join and Update query

So I want to update the action column to the value 'Insert' inside Table1, if the ids from Table1 and Table2 match but the UIDs dont.
Right now my query looks like
UPDATE Table1
SET Action = 'Insert'
FROM Table1
JOIN Table2 ON Table1.id = Table2.id
AND Table1.UID <> Table2.UID
This is setting the action to Insert even if the UIDs don't differ, can someone help me and explain why this is behaving this way?
My assumption is you have something like this:
Table1
id | UID | action
1 | 1 | bla
1 | 2 | bleck
1 | 3 | floop
Table2
id | UID | action
1 | 1 | bla
1 | 2 | bleck
1 | 4 | floop
And you hope to update the third row in Table1 because the UID isn't in Table2.
The problem is that the third row in Table2 matches all rows in Table1 on your condition: Table1.id = Table2.id AND Table1.UID <> Table2.UID
Which means that in this case, all rows in Table1 will be updated with Action = 'Insert'
I think you want to use NOT EXISTS():
UPDATE T1
SET Action = 'Insert'
FROM Table1 T1
WHERE NOT EXISTS (SELECT *
FROM Table2 T2
WHERE T1.id = T2.id
AND T1.UID = T2.UID)
Edit, more explanation on why the join fails:
This is a many to many join, meaning that the condition allows multiple rows from Table1 to match multiple rows from Table2
The easiest way to see this in action is to change your update to a select:
SELECT *
FROM Table1 T1
JOIN Table2 T2 on T1.id = T2.id
and T1.UID <> T2.UID
You may expect this to result in:
id | UID | action id | UID | action
1 | 3 | floop 1 | 4 | floop
But really it will result in:
id | UID | action id | UID | action
1 | 1 | bla 1 | 4 | floop
1 | 2 | bleck 1 | 4 | floop
1 | 3 | floop 1 | 4 | floop
This means that when you update you are hitting all the rows for id = 1 in Table1
If you put condition Table1.UID <> Table2.UID into WHERE clause, doesn't it solve your problem?
UPDATE Table1
SET Action = 'Insert'
FROM Table1
JOIN Table2 ON Table1.id = Table2.id
WHERE Table1.UID <> Table2.UID

update foreign key from key in another table

I need a query that will take the primary key from one table (table 2) and place it in a second table (table 1) as a foreign key. The database is Microsoft Access 2007. I tried the following query but it did not work:
update table1
set table1.table2ID = table2.ID
FROM table1 INNER JOIN table2 on table1.name = table2.name
The two tables are as follows:
Table 1:
ID | table2ID | Name
--------------------------
1 | | Name1
2 | | Name2
3 | | Name1
Table 2:
ID | Name
-----------------
1 | Name1
2 | Name2
I want the result to be:
Table 1:
ID | table2ID | Name
--------------------------
1 | 1 | Name1
2 | 2 | Name2
3 | 1 | Name1
Try this:
update table1
INNER JOIN table2 ON table1.name = table2.name
SET table1.table2ID = table2.ID
or this:
update table1, table2
SET table1.table2ID = table2.ID
WHERE table1.name = table2.name
Let me know which one works for you best.
U can try this :
update TABLE1 set tabel2id = TABLE2.Id from TABLE1 s
inner join TABLE2 s on u.name = s.NAME
It worked for me..