I have to update multiple columns with max value on the same table. A pseudo query will be like -
UPDATE PERMIT
SET P_ID = 'FIRST FIVE CHARACTERS WILL REMAIN SAME AND LAST few WILL BE REPLACED BY SELECT Max(prmt_appl_sq) FROM PERMIT SOME CONDITION',
prmt_appl_sq = 'AND NOW UPDATE WITH SAME MAX VALUE USED ABOVE'
WHERE
CONDITION
table data is like P_ID P_APPL_SQ CCode 2016-H-193-5 10 193 There is a unique constraint on column P_ID and the last part ***-5 which actually is value in P_APPL_SQ gets incremented every time a record is created with same other three values. for e.g when a new record is created having yr 2016 and type -H and Center 193 the P_ID will become ********-6. I have to update all the records having CCode =193 to 195 and update p_ID to 2016-H-195-"MAX value +1 for the combination of first three" and then also update P_APPL_SQ with same value.
I came up with the query but it is trying add same value on each row thus throwing unique constraint violation
UPDATE OSPDBA.OSPT001_PRMT_APPL
SET rted_to_org_cd = '55014040191',
PRMT_ID = SUBSTR(PRMT_ID, 0, 11) || (SELECT Max(prmt_appl_sq) + 1 FROM
OSPDBA.OSPT001_PRMT_APPL where rted_to_org_cd = '55014040191' and prmt_sbmt_yr
= 2016 and prmt_typ_cd = 'H'),
prmt_appl_sq = ((SELECT Max(prmt_appl_sq) + 1 FROM OSPDBA.OSPT001_PRMT_APPL
where rted_to_org_cd = '55014040191' and prmt_sbmt_yr = 2016 and prmt_typ_cd =
'H'))
WHERE
rted_to_org_cd IN ('55014040193','55014040195') and prmt_sbmt_yr = 2016;
How can we have unique max value for each record?