Get all tasks that link to a note (parent) and tasks that link to another note (child) that links to parent

What I’m trying to do

I am trying to query tasks that link to both a parent note and to its children. E.g., suppose there is a parent note [[Parent]] and a child note [[Child]] that links to the [[Parent]]. Basically, you can think of it as a task and a sub-task. “Note A” might look like

  • Do this on [[Parent]]

and “Note B” looks like

  • Do that on [[Child]]

I know how to get all the tasks that link to the [[Parent]] or that link to the [[Child]], but I cannot figure out how I can get the tasks that link to both (without explicitly specifying the names), and preferably group the output by whether it is a parent or a child, e.g,.

Parent
Note A

  • Do this on [[Parent]]

Child
Note B

  • Do this on [[Child]]

Things I have tried

My starting point is this code

TASK 
FROM [[]]
WHERE contains(text, this.file.name)
GROUP BY link as origin 
SORT origin.file.ctime DESC, text DESC