
  END AS action,
  parent_issues.id AS issue_id,
  notes.noteable_id AS child_work_item_id,
  notes.author_id AS user_id,
  system_note_metadata.created_at AS created_at,
  system_note_metadata.id AS system_note_metadata_id
FROM system_note_metadata
  INNER JOIN notes ON system_note_metadata.note_id = notes.id
  INNER JOIN issues as work_items ON work_items.id = notes.noteable_id,
LATERAL (
  -- This lateral join searches for the id of the parent issue.
  --
  -- When a child work item is added to its parent,
  --   "relate_to_parent" is recorded as `system_note_metadata.action`
  --    and a note records to which parent the child work item is added e.g, "added #1 (iid) as parent".
  --
  -- Based on the iid of the parent extracted from the note and using the child work item's project id,
  -- we can find out the id of the parent issue.
  SELECT issues.id
  FROM issues
  WHERE
    issues.project_id = work_items.project_id
    AND issues.iid = CASE WHEN system_note_metadata.action='relate_to_parent' THEN substring(notes.note from 'added #(\d+) as parent')::bigint
                      ELSE  substring(notes.note from 'removed parent \S+ #(\d+)')::bigint
                      END
  ) parent_issues
WHERE
  system_note_metadata.id IN (