Update column in table with value from another - sql

I need to update one column called Ident in table a to be a value from table b, as shown below. Both the change and keep values in table b are valid Idents which will appear in table a. I need to find the Change value in table b in table a, and then change the value in table a to the Keep value in table b.
Statement i'm using is:
update p set p.ident=t.keep
from pclscvq p inner join #tmp t on (p.ident=t.change)
where t.change=p.ident
Table B:
Change Keep
0004P 0004R
0004X 0004Y
00055 00056
00057 00058
0005B 0005C
0005K 0005L
0005Z 00060
00065 00066
0006X 0006Y
00070 00071
Very much stuck.
Using Advantage SQL

Related

SQL - Updating table from other table selecting update row based on "priority" setting. (PostgreSQL 11.0)

I'm trying to update the 'ea' column from A with the value of 'ea' in table B.
Table A has 'cfn' and 'ea' and has unique product entries.
Table B has 'cfn' and 'ea' and 'dchain' and may have multiple entries for the same product (different dchain).
Table B 'dchain' field is linked to Table C which has 'dc' and a 'prio' setting (integer).
The record to select from table B to update table A needs to be based on the priority of table C.
I have tried multiple options with limit and order but somehow I'm missing the right sequence as the result is always wrong.....
If I understand correctly, you can get the values to update using:
select distinct on (b.cfn) b.*
from b join
c
on b.dchain = c.dc
order by b.cfn, c.priority;
Note: This assumes that lower priorities are more important. If higher priorities are add desc to the second key.
Then you can incorporate this into an update:
update a
set a.ea = bc.ea
from (select distinct on (b.cfn) b.*
from b join
c
on b.dchain = c.dc
order by b.cfn, c.priority
) bc
where bc.cfn = a.cfn;

Update based of 1st and 2nd table

I have 3 tables.
1st table – MainTable - tableA
Have Project Name and description
A Apple
B Banana
C Carrot
2nd table - Table B
Child table :
A.10
A.20
A.30
B.10
B.20
B.30
Name of project (A, B, C) is Foreign key to table Child.
I have to update third table (table C) based on the
A …Apple
B…Banana
C…Carrot
This is Working fine with inner join.
Now when I am doing updates on code 10, 20 and 30 .. with ref to A, B and C of 10,20 and 30
It is not working.
Here is the query I wrote which is working fine to Update A, B and C
UPDATE C
SET c.[ProjectName] = a.[sysprojectname]
FROM TableC C
inner join tableA a ON c.[CostOBJProject]=a.[workpackageid]
Where c.[ProjectName] is null or c.[ProjectName]=''
So question is – I have to update table C based on value of table B with foreign key to table A.
in case I have value A and 10 in the tableC , then it should update the description in tableC .
Please check this link, this will help you
https://dev.mysql.com/doc/refman/5.7/en/update.html
I think you have problem on your syntax.
You can perform connected tables inside the UPDATE query.
UPDATE TableC, tableA
SET TableC.[ProjectName] = tableA.[sysprojectname]
Where tableA.connectcolumn = TableC.connectcolumn
AND (TableC.[ProjectName] is null or TableC.[ProjectName]='' )
With update syntax it has to be strict, therefore LEFT JOIN is not allowed on this scenario. It is equivalent to EQUI JOIN, an old style of joining.
SET clause commands the interpreter to manipulate only that column, even tho both columns were called.
With the complexity of your code you added OR logic command, would be nice to add parenthesis to properly utilize the logic.

SQL update tableA.column where the column and value is in a separate table

I have tableA with the columns ID, ColumnHeader, Value.
I'm trying to update tableB where ID, and the value in tableB.ColumnHeader with tableB.Value.
Essentially, the column headers for tableB are in a column in Table A and not column headers themselves.
I'm stuck on specifying the table name. For example, how do I run this query when I only have tablename.____ where the blank is in a column in a separate table?
update tableB set table.____ .....
As seen in the screenshot below, in Table B, 4 should become 1, and 8 should become 2, and 12 should become 3. Thanks so much.
this is a example from Northwind database:
UPDATE dbo.Products
SET dbo.Products.CategoryID = c.CategoryID
FROM Products
INNER JOIN dbo.Categories c ON dbo.Products.CategoryID = c.CategoryID
I am not getting which table and column you want to update with which table and column.
also relationship is not clear.
This doesn't make sense to me. Are you trying to update table B or A? Are there like values in table B and A that you want updated?
The basic code is as shown below:
update tableb
set column id = 'value you're updating'
where column id = 'value you're looking for' --always make this unique like a key etc.

Update one column where another column within same table equals a column in another table

I have a Query for retrieving data from one table where one column equals one specific value and one column equals a column in another table.
Like this:
SELECT a.IdOrdre, b.FR_Ordre
FROM Ordre a
INNER JOIN OrdreKategori b ON a.IdOrdre=b.FR_Ordre
WHERE b.FR_Kategori=57
I now would like to update the column FR_Kategori to another value than 57. How would I structure the UPDATE Query in order to achieve this?
Since you didn't tag your DBMS, and every one has different syntax for UPDATE..JOIN , here is an ANSI-SQL answer:
UPDATE OrdreKategori t
SET t.fr_kategori = Other_Val
WHERE EXISTS(SELECT 1 FROM Ordre s
where s.IdOrdre=t.FR_Ordre)
AND t.fr_kategori = 57

Insert data from another table based on condition, if not insert specific value PSQL

I have table A with an id column and a bunch of others. I also have table B which has an id column like table A and also another column called countries. However, not all id in table A are in table B. I want to insert a new column into table A called countries2 that basically inserts the countries from table B that corresponds to the ids in both tables. If a specific id from A does not exist in B, then it always puts in the VARCHAR 'none' into countries2.
So for example if Table A has the ids 1, 2, 3, 4 and table B looks like:
id--country
1---'foo1'
3---'foo2'
I want table A to become something like:
id--country2--other original data from table A
1---'foo1'--...
2---'none'--...
3---'foo2'--...
4---'none'--...
You need to first alter your TableA to add an extra column (i.e yourNewColumn) of type varchar/varchar2
to add the column try something like
ALTER TABLE TableA ADD COLUMN yourNewColumn varchar(10);
Then you can use something like below to update TableA
UPDATE TableA
SET yourNewColumn = ISNULL(TableB.countries, 'none')
FROM TableA
LEFT JOIN TableB ON TableA.id = TableB.id
The ISNULL function in PostgreSQL might be something like
SET yourNewColumn = CASE WHEN TableB.countries IS NULL THEN 'none' ELSE TableB.countries END