SELECT
    stackRun.stack_id,
    stackRun.tess_id,
    stackRun.skycell_id,
    stackRun.workdir,
    stackRun.reduction,
    stackRun.label,
    stackRun.state
FROM stackRun
JOIN stackInputSkyfile USING(stack_id)
JOIN warpRun USING(warp_id)
LEFT JOIN stackSumSkyfile USING(stack_id)
WHERE
    ((stackRun.state = 'new' AND stackSumSkyfile.stack_id IS NULL)
    OR (stackRun.state = 'update' AND stackSumSkyfile.fault = 0 AND stackSumSkyfile.quality = 0))
    -- WHERE hook %s
GROUP BY stack_id
HAVING COUNT(warpRun.state = 'full') = COUNT(stackInputSkyfile.warp_id)
