Postgresql – Postgres how to update first row in the child table for all parent entries

postgresqlupdate

I have a table parent table workflow and child table workflow_task having one to many mapping.

workflow`

workID workTitle
1800 vpInv1231
1801 vpInv1231

workflow_task

id task_Type workflow_id(fk)
1 null 1800
2 null 1800

Is there any way I can update all the first row in the child table with taskType as 'POC' and rest(from second to so on) as 'Appr'?.

Best Answer

Possible question interpretation:

UPDATE workflow_task
SET task_Type = CASE WHEN workflow_task.id = first_row.id
                     THEN 'POC'
                     ELSE 'Appr'
                     END
FROM ( SELECT MIN(id) id, workflow_id
       FROM workflow_task
       GROUP BY workflow_id ) first_row
WHERE workflow_task.workflow_id = first_row.workflow_id;

https://dbfiddle.uk/?rdbms=postgres_12&fiddle=08c60035f735ae557100dde8a7623008

workflow table not needed for this operation.