I'm trying to update a column in a table on every row from a query. The code I have is:
UPDATE CUSTOM_ISR_ASSET_DETAILS SET COUNTSYSTEMASSETS = ( Select Count(PARENT_ID) as COUNTSYSTEMASSETS
from PM_ENTITY
inner join
(SELECT
pm_entity.PM_ENTITY_ID,
response_text.response_text
FROM pm_entity
INNER JOIN response_text
ON pm_entity.pm_entity_id=response_text.parent_id
AND response_text.question_id = '000ZLIJCA0003MI82Z000UWLUTG000000P4V') TBL_StandardRollup
on PM_ENTITY.PM_ENTITY_ID = TBL_StandardRollup.PM_ENTITY_ID
WHERE (TBL_StandardRollup.response_text = 'Standard')
group by PARENT_ID);
What I'm trying to do is update each row with a count for each parent id but, obviously, this returns > 1 row so the update fails. So, I can't do this with this sql code.
How can I do this so that each row is updated with a count of parent ids?
Please try use merge statement. This would be something like:
merge into CUSTOM_ISR_ASSET_DETAILS c
using (/*YOUR SUB_QUERY*/) d
on (d.response_text = c./*what column 'standard' come from*/)
when matched then update set COUNTSYSTEMASSETS = d.COUNTSYSTEMASSETS;
I'm not able to define what is join condition. I guess on column where value 'standard'
comes from.
You have SELECT COUNT(x) GROUP BY x
I think what you need is to correlate that query and have it only return one row:
UPDATE CUSTOM_ISR_ASSET_DETAILS
SET COUNTSYSTEMASSETS = (
SELECT Count(PARENT_ID)
FROM whatever
WHERE whatever.PARENT_ID = CUSTOM_ISR_ASSET_DETAILS.PARENT_ID ) ;
That way the subquery is returning only a single row corresponding to the row you are updating.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.