-- find stopped warp runsthat have had all of their skycell's warped without
-- any faults
INSERT INTO warpComplete
SELECT
    warp_id,
    skycell_id,
    tess_id
FROM warpSkyCellMap
JOIN (SELECT warp_id, ignored
    FROM (SELECT 
            warpSkyCellMap.warp_id,
            warpSkyCellMap.skycell_id,
            warpSkyCellMap.tess_id,
            warpSkyfile.warp_id as w2,
            warpSkyfile.skycell_id as sc2,
            warpSkyfile.tess_id as t2,
            warpSkyfile.ignored,
            warpSkyfile.fault
        FROM warpRun
        JOIN warpSkyCellMap
            USING(warp_id)
        LEFT JOIN warpSkyfile
            USING(warp_id, skycell_id, tess_id)
        WHERE
            warpRun.state = 'stop'
        GROUP BY
            warpRun.warp_id
        HAVING
            SUM(warpSkyfile.fault) = 0
            AND COUNT(warpSkyCellMap.warp_id) = COUNT(warpSkyfile.warp_id)
            AND COUNT(warpSkyCellMap.skycell_id) = COUNT(warpSkyfile.skycell_id)
            AND COUNT(warpSkyCellMap.tess_id) = COUNT(warpSkyfile.tess_id)
        ) as Foo
    WHERE
        ignored = 0
    )  as Bar
    USING (warp_id)
