I am probably needing LEFT JOIN LATERAL here (and am completely lost)?
Or to switch to CTE as you suggest