-- This is part 2 of 2 of a query to get a list of exposures on which magic may be performed
-- This follows magictool_definebyquery_select_part1.sql
    GROUP BY
        exp_id
    ) AS magicSkycellNums
JOIN (
    -- Number of completed diffs for an exposure
    SELECT
        exp_id,
        COUNT(diff_id) AS num_done
    FROM magicBestDiffs
    GROUP BY
        exp_id
    ) AS magicDiffNums USING(exp_id)
LEFT JOIN magicRun USING(exp_id)
WHERE
    num_done = num_todo
    AND magicRun.magic_id IS NULL
