SELECT DISTINCT
    stackRun.stack_id,
    stackRun.tess_id,
    stackRun.skycell_id,
    stackRun.workdir,
    stackRun.label,
    stackRun.state
FROM stackRun
JOIN stackInputSkyfile
    USING(stack_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))
