Update fields with data from a second table - sql

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

Related

How do I update the duplicate values and their foreign key in the child table?

I have 2 tables
Table A
Table B
Table A has 2 columns
ID, CellNo
Now Table B is the CHILD table of A.
In Table B, we have
ID, TableA_CellNO, TableA_ID.
I have duplicate CellNo's in table A and in Table B those two records exist but TableA_ID is updated with the wrong id's.
I need to Update all of the TableA_ID's with the correct values against the duplicate CellNo.
I have written a query to update by matching CellNo in both table but that doesn't work.
UPDATE TableA AS R
INNER JOIN TableB AS P
ON R.CellNo = P.TableA_CellNo
SET P.TableA_ID = R.ID
I'm assuming you want to update table A's Id to table B's latest Id, meaning that the last CellNo record inserted into B is the correct one.
This may not be the most efficient but it should work.
UPDATE a
SET a.Id = b.Id
FROM TableA a
JOIN TableB b
ON b.Id = a.Id
AND b.Id =
(
SELECT MAX(ib.Id) FROM TableB ib WHERE ib.CellNo = a.CellNo
);
You 've a syntax error there. I suppose you 're looking for
UPDATE P
SET P.TableA_ID = R.ID
FROM TableA R
JOIN TableB P ON R.CellNo = P.CellNo;
--WHERE <Some Conditions> if needed

SQL Get rows that doesn't appear in another table

I have this SQL problem: I have tables A and B. Table A has columns id and name, Table B amount and id which is a foreign key to table A.id.
I need to return all table A rows that don't have their id stored in table B. Any ideas?
So the complete opposite is:
SELECT *
FROM a
LEFT OUTER JOIN b ON a.id = b.id;
Here row what I need is left out of result
Just add a where clause:
SELECT a.*
FROM a LEFT OUTER JOIN
b
ON a.id = b.id
WHERE b.id IS NULL;
You can also use NOT EXISTS:
select a.*
from a
where not exists (select 1 from b where b.id = a.id);
In most databases, the two methods typically have similar performance.

Query two tables - return Items that are not in both

I have two tables like this :
TableA
Id | ProjectId | JobId
TableB
Id | Title
I want to write a query returning TableB Ids that are not in TableA with ProjectId = 2.
I've written a query like
Select B.Id
From TableB B
Right Join TableA A On B.Id = A.JobId
Where B.JobId Is Null And A.ProjectId = 2
But it returns zero output.
Thanks
I would use EXISTS here:
SELECT b.Id
FROM TableB b
WHERE NOT EXISTS (SELECT 1 FROM TableA a WHERE a.JobId = b.Id AND a.ProjectId = 2);
Reading in English terms, the above says to select every Id in TableB such that we cannot find an equal Id in TableA whose ProjectId is also 2.
Query edited

SQL "Where exists" with multiple tables with aliases

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);

Update a table with records from another table

I am trying to update my table 1 with some field value from table 2 based on a condition. But am unable to get the proper query. Here's the condition:
I have a table 1 with date field which should be updated from table 2 date field. The condition is that the id of table 1 should be equal to id of table 2 (Table 2 has id of table 1 as FK). Another condition should be there is a varchar field in table 2 which should have specific value, say "Test". Wherever the value of field in table 2 is "Test" I want to update the date of with that record in table 1 date field. But there is another catch. It may be possible that more than 1 records for same id in table 2 can have value as "Test"
I was trying the query as:
UPDATE A
SET A.Date = Max(B.[Date])
FROM Table1 A
INNER JOIN Table2 B ON A.ID = B.FK_ID
WHERE (B.Changed LIKE 'Test')
AND A.Date IS NULL
But this is not working as sql does not allow Max in update when there is no group by. Please help. A bit urgent.
You need to create another inner join where the ID's of Table1 are coupled with the maximum dates of Table2 like so:
UPDATE Table1
SET Date = BDate.MaxDate
FROM Table1 A
INNER JOIN Table2 B ON A.ID = b.FK_ID
INNER JOIN (
SELECT A.ID, [MaxDate] = MAX(B.Date)
FROM Table1 A
INNER JOIN Table2 B ON A.ID = b.FK_ID
GROUP BY A.ID
) BDate ON BDate.ID = A.ID
WHERE B.Changed LIKE 'Test'
A.Date IS NULL
You could always use subqueries:
UPDATE Table1 a SET
[Date] = (SELECT MAX([Date]) FROM Table2 b WHERE a.ID = b.FK_ID AND b.Changed LIKE 'Test')
WHERE [Date] IS NULL