SELECT * FROM
    (SELECT 
        diffRun.diff_id,
        diffRun.skycell_id,
        diffRun.tess_id,
        0 as stack_id,
        warpSkyfile.warp_id,
        warpSkyfile.uri,
        warpSkyfile.path_base,
        diffInputSkyfile.template,
        rawExp.camera
    FROM diffRun
    JOIN diffInputSkyfile
        USING(diff_id)
    JOIN warpSkyfile
        ON  diffInputSkyfile.warp_id    = warpSkyfile.warp_id
        AND diffInputSkyfile.skycell_id = warpSkyfile.skycell_id
        AND diffInputSkyfile.tess_id    = warpSkyfile.tess_id
    JOIN warpRun
        ON diffInputSkyfile.warp_id = warpRun.warp_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
    WHERE
        diffRun.state = 'new'
        AND warpRun.state = 'full'
        AND fakeRun.state = 'full'
        AND camRun.state = 'full'
        AND chipRun.state = 'full'
        -- where hook %s
    UNION
    SELECT 
        diffRun.diff_id,
        diffRun.skycell_id,
        diffRun.tess_id,
        stackSumSkyfile.stack_id,
        0 as warp_id,
        stackSumSkyfile.uri,
        stackSumSkyfile.path_base,
        diffInputSkyfile.template,
        rawExp.camera
    FROM diffRun
    JOIN diffInputSkyfile
        USING(diff_id)
    JOIN stackSumSkyfile
        ON  diffInputSkyfile.stack_id = stackSumSkyfile.stack_id
    JOIN stackInputSkyfile
        ON diffInputSkyfile.stack_id = stackInputSkyfile.stack_id
    JOIN warpRun
        ON stackInputSkyfile.warp_id = warpRun.warp_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
    WHERE
        diffRun.state = 'new'
        AND warpRun.state = 'full'
        AND fakeRun.state = 'full'
        AND camRun.state = 'full'
        AND chipRun.state = 'full'
        -- where hook %s
    ) as Foo
