Update column when a match in another table is found - sql

I'm try to update a column in table B with a value in table A where the passport_no match.
Below is my sql query.
update tabel_b
set b.country_id = a.national_id
from table_a a
join tabel_b b on b.passport_no = a.passport_no
where a.is_deleted = false;
On executing, I get ERROR: column "b" of relation "tabel_b" does not exist
How can I go about this please.

The immediate error is that you can't qualify the column you want to update. So it should be set country_id = ...
The bigger problem is however your join.
As documented in the manual
Do not repeat the target table as a from_item unless you intend a self-join
So the correct statement most probably should be:
update tabel_b
set country_id = a.national_id
from table_a a
where b.passport_no = a.passport_no
and a.is_deleted = false;

Related

Updating column in table using left join from another table in SQL

I want to update column 'location_cost' under 'animals' table basis cost stated under 'location_costs' table. Primary key is location to join both tables. I have tried the following code but it gives me syntax error.
//UPDATE animals.*
SET animals.location_cost = location_costs.costs
FROM animals
LEFT JOIN location_costs
ON animals.location = location_costs.location;//
Error: syntax error at or near "SET"
SET animals.location_cost = location_costs.costs
I am attaching a picture which gives an idea for the tables and columns here:
Tables View
I am not able to decipher the error and would appreciate if someone can help me with this code.
Thank you.
If you want to update only the rows in animals that have a matching location in location_costs then use this syntax:
UPDATE animals
SET location_cost = location_costs.costs
FROM location_costs
WHERE location_costs.location = animals.location;
If you want to update all the rows of animals (even the rows without a a matching location in location_costs will be updated to null), then use a correlated subquery:
UPDATE animals
SET location_cost = (
SELECT location_costs.costs
FROM location_costs
WHERE location_costs.location = animals.location
);
If animalid is a column with unique values, try using an alias as follows:
Update animals
Set location_cost = location_costs.costs
From animals As a Left Join location_costs On (a.location = location_costs.location)
Where animals.animalid = a.animalid;

Update table column based on two other table values

I need to update a column in one of my tables based on data from 2 other tables.
So I want the column isAvailable, in the table questionObjectives, to be set to 1 based on 2 conditions and this is what I have:
UPDATE
dbo.questObjectives
SET
isAvailable = 1
FROM
dbo.questObjectives qo
INNER JOIN
dbo.dungeonList dl
ON
qo.questID = dl.questID
WHERE dl.dungeonType = 17
AND qo.objectiveID IN(SELECT objectiveID FROM gameMissions)
So to translate, isAvailable should be set to 1 if:
the linked dungeonList type is 17
the questionObjectives objectiveID is in the table gameMissions
So I thought I had my logic right, but I keep getting this error:
'invalid column name isAvailable.'
But it is there. It is in the questionObjectives table so I'm not sure what I'm doing wrong.
Any ideas?
Thanks!
Is this what you want?
update qo
set qo.isavailable = 1
from questObjectives qo
inner join dungeonList dl on qo.questID = dl.questID
where
dl.dungeonList = 17
and exists (select 1 from gameMissions gm where gm.objectiveID = qo.objectiveID)
The main problem with your query is that you have target table questObjectives both in the update and from clauses; you should have it just once, in the from clause, and then refer to the alias in the update clause.
I also rewrote the in condition as a correlated subquery with exists - the logic is the same, but this might perform better.

How can I update table A with data from table C when A and C are linked via table B

I have three tables(A, B, and C) and would like to update null values in A.appointment_id with values in C.tc_appointment_id. Table A and C can be joined via table B. Blue arrows represent joins, the red arrow represents the update I am trying to achieve.
I am able to join the three tables together and have tried to modify my select statement into an update. My successful select statement and unsuccessful update follow.
--Working select
select A.tc_ASN_id,A.appointment_id, B.appt_obj_id, B.appointment_id, C.appointment_id, C.tc_appointment_id from B
join A on B.appt_obj_id = A.asn_id
join C on C.appointment_id = B.appointment_id
where C.appt_status < '9' and A.appointment_id is null;
--Update attempt that ends with SQL Error: ORA-00933: SQL command not properly ended
update asn set appoinmtent_id = ilm_appointments.tc_appointment_id
join ilm_appointment_objects on ilm_appointment_objects.appt_obj_id = asn.asn_id
join ilm_appointments on ilm_appointments.appointment_id = ilm_appointment_objects.appointment_id
where ilm_appointments.appt_status < '9' and asn.appointment_id is null;
The expected result is to update all null values for A.appointment_id to be updated with the values from C.tc_appointment_id.
UPDATE...JOIN syntax is not supported in Oracle. You could use a correlated subquery instead.
Consider:
UPDATE A
SET A.appointment_id = (
SELECT C.tc_appointment_id
FROM B
INNER JOIN C ON C.appointment_id = B.appointment_id
WHERE B.appt_obj_id = A.asn_id
)
WHERE A.appointment_id IS NULL;
Beware that the subquery must return a unique record, otherwise you will get an error like "subquery returned more than one row". Given your sample data this seems to be the OK.

How to update previous records without updating max record in Netezza

Hoping someone can be of assistance. I am trying to do an update statement. I have a table that has the columns:
BUS_NBR_SK, BUS_NBR, EFF_DT, ENT_TMPSTP, EXP_DT.
Primary keys are BUS_NBR_SK and BUS_NBR. There is only one table here.
These are inserts into a table and I need to update the EXP_DT on the older records and leave the most recent one alone by using the max(ENT_TMSTP) I was going to just do an update with a SET b.EXP_DT = current_timestamp where EXP_DT IS NULL however that won't work because the most recent record will also be assigned an expire date.
Any ideas how that could work?
I have tried the following but it was updating everything with the max effective date.
UPDATE TABLE b
SET b.EXP_DT = (SELECT MAX(a.EFF_DT)
FROM TABLE A
INNER JOIN TABLE B
ON A.BUS_NBR_SK = B.BUS_NBR_SK
AND A.ENT_TMSTP = B.ENT_TMSTP
AND A.BUS_NBR = B.BUS_NBR)
WHERE EXP_DT IS NULL
and ENT_TMSTP != (select max(c.ENT_TMSTP)
from table C)
Thank you so much!
example of the fields in the table with sample data.
I'm not sure if this will fix your problem, but using a correlated subquery will at least get the set logic correct:
UPDATE table b
SET b.EXP_DT = (SELECT MAX(a.EFF_DT)
FROM TABLE A
WHERE A.BUS_NBR_SK = B.BUS_NBR_SK AND
A.ENT_TMSTP = B.ENT_TMSTP AND
A.BUS_NBR = B.BUS_NBR
)
WHERE b.EXP_DT IS NULL AND
b.ENT_TMSTP <> (SELECT MAX(c.ENT_TMSTP) FROM table C);
I'm not sure if this fixes your overall problem, though.
If you want to update b.EXP_DT with the date at a.EFF_DT for the newest record for a specific key of BUS_NBR_SK, ENT_TMSTP and BUS_NBR you can create a Group by view for A or you can declare it inside the update query as shown below:
UPDATE table b
SET b.EXP_DT = A_Group.Max_EFF
FROM TABLE A INNER JOIN
(SELECT BUS_NBR_SK, ENT_TMSTP, BUS_NBR, MAX(A.EFF) as Max_EFF
FROM B
GROUP BY BUS_NBR_SK, ENT_TMSTP, BUS_NBR) AS A_Group ON
A_Group.BUS_NBR_SK = b.BUS_NBR_SK AND
A_Group.ENT_TMSTP = b.ENT_TMSTP AND
A_Group.BUS_NBR = b.BUS_NBR
WHERE b.EXP_DT IS NULL AND
b.ENT_TMSTP <> (A_Group.Max_EFF);
Hope it helps.
Sergio

Update using Inner Join throwing syntax error

I would like to update columns in Table A based on values in Table B. Using below format, but getting syntax error.
update TableA
set
TableA.MOdule_id =TableB.MOdule_id
from TableA
inner join
TableB
on TableA.end_Slot_id =TableB.Slot_Id
where TableA.Slot_Id = 'AAA';
It would be great help, if anyone can help on this.
A quick search for "informix sql update" returns two reference manual pages that describe the syntax for the UPDATE command. Neither one indicates support for the nonstandard FROM clause.
Standard SQL uses a correlated subquery for the purpose. Your query should look something like
update TableA
set MOdule_id =
(select TableB.MOdule_id
from TableB
on TableA.end_Slot_id = Slot_Id)
where Slot_Id = 'AAA'
and exists (
select 1
from TableB
on TableA.end_Slot_id = Slot_Id
and TableA.Slot_Id = 'AAA'
);
The EXISTS clause ensures that only rows that exist in B are applied to A. Without it, any missing rows would be updated to NULL.