SELECT DISTINCT
    warpSkyCellMap.warp_id,
    warpImfile.warp_skyfile_id,
    warpSkyCellMap.skycell_id,
    warpSkyCellMap.tess_id,
    warpRun.fake_id,
    warpRun.state,
    camRun.cam_id,
    rawExp.camera,
    rawExp.exp_tag,
    warpRun.workdir
FROM warpRun
JOIN warpSkyCellMap
    USING(warp_id)
JOIN warpImfile
    ON warpSkyCellMap.warp_id = warpImfile.warp_id
    AND warpSkyCellMap.skycell_id = warpImfile.skycell_id
JOIN fakeRun
    USING(fake_id)
JOIN camRun
    USING(cam_id)
JOIN chipRun
    USING(chip_id)
JOIN chipProcessedImfile
    USING(chip_id)
JOIN rawExp
    ON chipRun.exp_id = rawExp.exp_id
LEFT JOIN warpSkyfile
    ON warpRun.warp_id = warpSkyfile.warp_id
    AND warpSkyCellMap.skycell_id = warpSkyfile.skycell_id
    AND warpSkyCellMap.tess_id = warpSkyfile.tess_id
LEFT JOIN warpMask
    ON warpRun.label = warpMask.label
WHERE
    ((warpRun.state = 'new'
        AND warpSkyfile.warp_id IS NULL
        AND warpSkyfile.skycell_id IS NULL
        AND warpSkyfile.tess_id IS NULL)
    OR (warpRun.state = 'update'
        AND warpSkyfile.data_state = 'cleaned')
    )
    AND fakeRun.state = 'full'
    AND camRun.state = 'full'
    AND chipRun.state = 'full'
    AND warpMask.label IS NULL
    AND warpSkyCellMap.fault = 0
