SELECT DISTINCT
    exp_name, -- return should match pzDownloadExp
    camera,
    telescope,
    state
FROM (
    SELECT
        pzDownloadImfile.*,
        pzDownloadExp.state,
        summitExp.imfiles
    FROM pzDownloadExp
    JOIN summitExp
        USING(exp_name, camera, telescope)
    LEFT JOIN pzDownloadImfile
        USING(exp_name, camera, telescope)
    LEFT JOIN newExp
        ON pzDownloadExp.exp_name = newExp.tmp_exp_name
        AND pzDownloadExp.camera = newExp.tmp_camera
        AND pzDownloadExp.telescope = newExp.tmp_telescope
    WHERE
        pzDownloadExp.state = 'run'
        AND newExp.tmp_exp_name IS NULL
        AND newExp.tmp_camera IS NULL
        AND newExp.tmp_telescope IS NULL
    GROUP BY
        pzDownloadExp.exp_name,
        pzDownloadExp.camera,
        pzDownloadExp.telescope
    -- it doesn't matter which field in pzDownloadImfile we count as we've
    -- already Download a group by
    HAVING
        COUNT(pzDownloadImfile.exp_name) = summitExp.imfiles
        AND SUM(pzDownloadImfile.fault) = 0
    ) as Foo
