I have some Problem, there are two Tables, they communicate with the value ID.
Now I will set the value from Column a in Table A with the value 'Nein', but only if the value of the column b in Table B is '0' and
if a.id = b.id.
How can I do that?
Thanks
You'll need to make a Join in the Update Statement like:
UPDATE a set ColumnA='Nein' from TableA a inner join TableB b on a.id=b.id WHERE b.ColumnB='0'
please try the below query . Since there here i am not sure that id is primary column in table , i have used "in" clause .
update A
set A.a ='Nein'
where A.id in ( select A.id from A ,B
where A.id = B.id and B.b='0')
Try with
update A a set a.a='Nein'
where a.id in (select b.id from B b where B.b='0' and a.id=b.id);
Related
I am trying to update the column (REPT_IND) from table A to the value in table B where A.ID = B.ID and some conditions in table B.
There are some duplicates in table B, but nonetheless the REPT_IND is the same and I still need the value.
How can I do this on Oracle? Any tips are appreciated thank you!
The Following code has the Error:
ORA-01427: single-row subquery returns more than one row
Code:
UPDATE A
SET REPT_IND= (
SELECT B.REPT_IND
FROM B
INNER JOIN A
ON B.ID = A.ID
where A.ID = B.ID
and B.job_type = 'P'
and B.FT_PT is not null
);
You can try also merge statement:
merge into a
using (
select a.id,max(b.rept_ind) rept_ind
from a left join b on a.id=b.id
where b.job_type = 'p'
and b.ft_pt is not null
) b
on (a.id=b.id)
when matched then update
set a.rept_ind=b.rept_ind;
Or if you do not want to set a.rept_ind to null if there is no relevant rows in b:
merge into a
using (
select b.id, max(b.rept_ind) rept_ind
from b
where
b.job_type = 'p'
and b.ft_pt is not null
group by b.id
) b
on (a.id=b.id)
when matched then update
set a.rept_ind=b.rept_ind;
Consider:
update a
set rept_ind= (
select max(b.rept_ind)
from b
where
a.id = b.id
and b.job_type = 'p'
and b.ft_pt is not null
);
There is no need to join table a again in the subquery - a correlation clause is enough. And you can work around possible duplicates by turning on aggregation, which guarantees that only one row will be returned.
You could also use select distinct instead of select max(...) in the subquery. This is somehow more accurate since it does ensure that the multiple rows have the same rept_ind (it they do not, then you would still get the ORA-01427 error).
Just use a correlated subquery . . . and do not repeat the table reference in the subquery:
UPDATE A
SET REPT_IND = (SELECT B.REPT_IND
FROM B
WHERE B.ID = A.ID AND
B.job_type = 'P' AND
B.FT_PT is not null AND
rownum = 1
);
I have a database in PostgreSQL and I'm having some problems updating some values, let me explain.
I have three tables.
Table A:
id | itemID | Value
Table B:
Name | Value
Table C:
itemID | Name
I need to update the value field on table A with the value from table B where the itemId from the tables A and C are equal.
I don't really know how to explain, please ask me anything so I can explain it better
You need a join in the UPDATE statement:
update tablea
set "Value" = b."Value"
from tableb b inner join tablec c
on c.name = b.name
where c."itemID" = tablea."itemID";
See the demo.
You can use Update statement with a Common Table Expression, and bring the results directly by adding returning clause at the end.
with t0 as
(
select c.itemID , b.value
from tableC c
left join tableB b on b.name = c.name
)
update tableA a
set value = t0.value
from t0
where a.itemID = t0.itemID
returning a.*;
Demo
I need to copy the values of column a of table A into column b of Table B.
Is this statement correct?
UPDATE
TableA,
TableB
SET
TableB.b = TableA.a
WHERE
TableA.Id = TableB.Id
If the column of the destination is empty and you do not need to match something just use this
INSERT INTO DestinationTb ([ColumnName])
SELECT [ColumnNameToTransfer] FROM [SourceTable]
if not just with a join
update A
SET A.Columnname = B.ColumnNameToTransfer
from DestinationTb A
INNER JOIN
SourceTable B
ON
--HERE ADD YOUR MATCHING FOR EXAMPLE
A.ID = B.ID;
or with a subquery
UPDATE DestinationTb
SET ColumnName = (
SELECT ColumnNameToTransfer
FROM SourceTable
--HERE ADD YOUR MATCHING FOR EXAMPLE
WHERE SourceTable.id = DestinationTb.id
);
One solution is to use FROM and JOIN behind UPDATE
UPDATE A
SET A.a= B.b
FROM TableA A
JOIN TableB B ON A.ID = B.ID
I am not sure if this can be done with a single JOIN, but I basically have two tables with an ID column in common. To make it simple I'll say Table A just contains an ID while Table B contains an ID and Code. There is a 1:M relationship between Table A and Table B, however it's also possible an ID from Table A is not contained in Table B at all. I was hoping to have a query return every ID that exists in Table B within a particular code range, or does not exist in Table B at all.
I tried using a LEFT JOIN with something like:
SELECT A.id FROM A LEFT JOIN B ON A.id = B.id AND b.code BETWEEN '000' AND '123'
But, this still gives me the IDs that exist in Table B outside of the code range.
Use a left join, and filter the result to contain the codes in the range, and also the lines where there is no matching record in table B:
select
A.id
from
A
left join B on B.id = A.id
where
B.code between '000' and '123' or B.id is null
What about
SELECT id FROM A LEFT JOIN B ON A.id = B.id
WHERE b.code IS NULL OR b.code BETWEEN ' ' AND '123'
Example query:
Select id, id_dtm
From tableA
Where exists (
Select 1
From tableB b, tableC c, tableD d
Where b.id = id
And b.id_dtm = id_dtm
And b.id = c.id
And c.id = d.id);
The problem with the above query is that all 4 tables have columns named id and id_dtm.
When i run it, i get an error saying that the columns ORA-00918: column ambiguously defined
I could have fixed by using an alias in tableA but the problem is that the query is generated dynamically. The where exists portion is generated somewhere else and the bit before it is merged later so i cant use an alias as it is now.
Is there any way i can use id and id_dtm from tableA inside the where exists clause without using an alias for tableA?
Database is Oracle10G
Write the table name tableA:
Select id, id_dtm
From tableA
Where exists (
Select 1
From tableB b, tableC, tableD
Where tableB.id = tableA.id
And tableB.id_dtm = tableA.id_dtm
And tableB.id = tableC.id
And tableC.id = tableD.id)
I don't know your exact setup but why can't you set an alias on the outer table? It doesn't have to reflect the actual table used, just alias it with "outer" or something. The use that in the inner query, you already know that id exists in whatever table is used outside so outer.id would work fine.
The fields in the subquery which refer to tableA (i.e. id and id_dtm) also exist in the other tables, and so they are ambiguous. Solve this by prefixing those with the alias given to tableA:
Select A.id, A.id_dtm
From tableA A
Where exists (
Select 1
From tableB b, tableC c, tableD d
Where b.id = A.id
And b.id_dtm = A.id_dtm
And b.id = c.id
And c.id = d.id);