SQL update set table if value in table A is equals to value in table B - sql

this query is working fine.
UPDATE data
SET unit_id='a3110a89'
WHERE unit_id='7d18289f';
Now, I need to run this query over 30 times
so I made csv file import it to the DB with this command:
COPY mytable FROM 'D:/test.csv' WITH CSV HEADER DELIMITER AS ','
Now I have table called my table with 2 columns OLD and NEW
i want to search the table "data" in column unit_id anywhere there if the value equals to the value in table "mytable.old" replace it with the value "mytable.new" on the same row.
I tried to run this query but I get an error:
UPDATE data
SET unit_id=(SELECT mytable."old" FROM public.mytable)
WHERE unit_id=(SELECT mytable."new" FROM public.mytable)
error:
more than one row returned by a subquery used as an expression
I think i'm just trying to do it in the wrong way...
thx for the help!
by the way Im using PostgreSQL

Your subqueries need to be correlated to the outer update:
UPDATE data
SET unit_id = (SELECT mytable."new" FROM public.mytable where data.old = mytable.old)
WHERE unit_id in (SELECT mytable."old" FROM public.mytable);
That is, set the unit_id to the "new" value, when you find the "old" value in the table.

Can you try like this,
UPDATE data A
SET A.unit_id=B.old
FROM (SELECT mytable."old",mytable."new" FROM public.mytable) B
WHERE A.unit_id=B.new

UPDATE data A
SET unit_id = B."old"
FROM public.mytable B
WHERE A.unit_id = B."new"
;
BTW: it looks like you also have old and new swapped in your question. Do you really want A's value to be set to B's old field?

Related

Update date parameter in multiple table using SQL

I need to update the below dates for multiple records in a table. Each record has a unique Id call object_id. I have used the below SQL to update one record but would like to know if I can bulk update them. I constantly receive these requests to update the date. This time it is only 6 records but usually, I get the request to update even 50 records.
This is the data that I received
This is the data in the database table.
I need to update the CASE_DETAIL table by adding the judgment date into JUDGMENT_DATE COLUMN which is currently null.
update case_detail cd set CD.JUDGMENT_DATE = '18/DEC/1998' where CD.OBJECT_ID = 5091449
Any advice in the direction of creating a SQL script that would bulk update the date column in multiple records would be of great assistance.
If your new values are arriving in a table, you can update the case_detail table by doing:
update case_detail cd
set judgment_date = (select r.judgement_date
from received r
where cd.object_id = r.object_id
)
where exists (select 1
from received r
where cd.object_id = r.object_id
);
Please try this.
UPDATE case_detail cd SET CD.JUDGMENT_DATE = '18/DEC/1998' WHERE CD.OBJECT_ID IN (SELECT OBJECT_ID FROM #ReceivetableName) AND CD.JUDGMENT_DATE IS NULL

using RIGHT to get data from column and update to add result to new column

I have a trouble to get this right.
I get data with this query
Select RIGHT(RTRIM(Nimi), 3) Riik from TABLE
But when I use this in update, then it has more then one result to insert.
So when I use it like this :
update #temp_table
set veerg2 = (Select RIGHT(RTRIM(nimi), 3) nimi2
from #temp_table a
where a.nimi is not NULL )
Then I get error on that, but I need to get these three last chars into new column.
Need help on that.
Your syntax looks like SQL Server. The correct query is most likely:
update a
set veerg2 = RIGHT(RTRIM(nimi), 3)
from #temp_table a
where a.nimi is not NULL ;
The two changes are:
The table alias defined in the from clause is used in the update rather than the table name.
The select is removed from the set. It is unnecessary.
You can also write this without the from:
update #temp_table
set veerg2 = RIGHT(RTRIM(nimi), 3)
where nimi is not NULL ;

Update redshift column value with modified data from other column

I have a redshift table which is used for tracking, and as a result its pretty huge. I need to update one column after applying some text operations and extracting a value from another column.
The query that I have managed to write works only for one row.
UPDATE schema.table_name SET data_id = (SELECT split_part(regexp_substr(data_column,'pattern=[^&]*'),'=',2)::BIGINT FROM schema.table_name where id = 1620) WHERE id = 1620;
How do I get it to work for every row in the table.
UPDATE
schema.table_name
SET
data_id = SPLIT_PART(REGEXP_SUBSTR(data_column, 'pattern=[^&]*'),'=',2)::BIGINT;
Just don't put WHERE id = 1620; at end of update query.
Updates are not efficient in Redshift. If you have a huge table and you intend to update every single row, you should instead copy the data (with the updated column) to a new table and then flip them.

How to Update a Single record despite multiple Occurances of the same ID Number?

I have a table that looks like the below table:
Every time the user loan a book a new record is inserted.
The data in this table is derived or taken from another table which has no dates.
I need to update this tables based on the records in the other table: Meaning I only need to update this table based on what changes.
Example: Lets say the user return the book Starship Troopers and the book return is indicated to Yes.
How do I update just that column?
What I have tried:
I tried using the MERGE Statement but it works only with unique rows of data, meaning you get an error if the same ID appears more than once.
I also tried using a basic UPDATE Statement and a JOIN but that's not going well.
I am asking because I have ran out of ideas.
Thanks for reading
If you need to update BooksReturn in target table based on the same column in source table
UPDATE t
SET t.booksreturn = s.booksreturn
FROM target t JOIN source s
ON t.userid = s.userid
AND t.booksloaned = s.booksloaned
Here is SQLFiddle demo
You can do this by simple Update & Insert statement.....
Two table A & B
From B you want to insert data into A if not exists other wise Update that data....
,First Insert into temp table....
SELECT *
INTO #MYTEMP
FROM B
WHERE BOOKSLOANED NOT IN (SELECT BOOKSLOANED
FROM A)
,Second Check data and insert into A.
INSERT INTO A
SELECT *
FROM #MYTEMP
And at last write one simple update statement which update all data of A. If any change then it also reflect to that data otherwise data as it is.
You can also update from #MYTEMP table.

Convert value in all rows to VARBINARY(50)

I want to upgrade all columns in a table, what I am aiming to do is retreive a column from the row it will update then update it, something like:
update works
set encrpyted_item_no = (CAST(RTrim((
select unencrypted_item_no
from works
where name = name
) AS VARBINARY(50))
I know that query is wrong, it's just an example to show you what I am aiming to do.
I want it to select the column unencrypted_item_no from its row then update that same row with the data it gets from unencrypted_item_no, doing this for the whole table.
How would I accomplish this?
You shouldn't need to do a sub-select, referencing the other column in the set will work on a row by row basis, ie:
UPDATE works
SET encrpyted_item_no = CAST(RTrim(unencrypted_item_no) AS varbinary(50))
Shouldn't this be enough ?
update works
set encrypted_item_no = CAST(RTrim(unencrypted_item_no) AS VARBINARY(50))