SELECT * FROM (
SELECT
    distRun.dist_id,
    stage,
    stage_id,
    rawImfile.class_id AS component,
    clean,
    rawExp.camera,
    outroot,
    rawImfile.uri as path_base,
    chipProcessedImfile.path_base as chip_path_base,
    NULL as state,
    NULL as data_state,
    rawImfile.magicked
FROM distRun
JOIN rawExp ON exp_id = stage_id
JOIN rawImfile using(exp_id)
JOIN chipProcessedImfile
    ON distRun.chip_id = chipProcessedImfile.chip_id
    AND rawImfile.class_id = chipProcessedImfile.class_id
LEFT JOIN distComponent 
    ON distRun.dist_id = distComponent.dist_id 
    AND rawImfile.class_id = distComponent.component
WHERE
    distRun.state = 'new'
    AND distRun.stage = 'raw'
    AND distComponent.dist_id IS NULL

-- chip stage
UNION
SELECT
    distRun.dist_id,
    stage,
    stage_id,
    chipProcessedImfile.class_id AS component,
    clean,
    rawExp.camera,
    outroot,
    chipProcessedImfile.path_base,
    chipProcessedImfile.path_base as chip_path_base,
    chipRun.state,
    chipProcessedImfile.data_state,
    chipProcessedImfile.magicked
FROM distRun
JOIN chipRun ON chipRun.chip_id = distRun.stage_id
JOIN rawExp using(exp_id)
JOIN chipProcessedImfile ON chipProcessedImfile.chip_id = stage_id
LEFT JOIN distComponent 
    ON distRun.dist_id = distComponent.dist_id 
    AND chipProcessedImfile.class_id = distComponent.component
WHERE
    distRun.state = 'new'
    AND distRun.stage = 'chip'
    AND distComponent.dist_id IS NULL
UNION
SELECT
    distRun.dist_id,
    stage,
    stage_id,
    warpSkyfile.skycell_id AS component,
    clean,
    rawExp.camera,
    outroot,
    warpSkyfile.path_base,
    NULL as chip_path_base,
    warpRun.state,
    warpSkyfile.data_state,
    warpSkyfile.magicked
FROM distRun
JOIN warpRun ON stage_id = warp_id
JOIN warpSkyfile using(warp_id)
JOIN fakeRun USING(fake_id)
JOIN camRun USING(cam_id)
JOIN chipRun ON camRun.chip_id = chipRun.chip_id
JOIN rawExp using(exp_id)
LEFT JOIN distComponent 
    ON distRun.dist_id = distComponent.dist_id 
    AND warpSkyfile.skycell_id = distComponent.component
WHERE
    distRun.state = 'new'
    AND distRun.stage = 'warp'
    AND distComponent.dist_id IS NULL
UNION
SELECT
    distRun.dist_id,
    stage,
    stage_id,
    diffSkyfile.skycell_id AS component,
    clean,
    rawExp.camera,
    outroot,
    diffSkyfile.path_base,
    NULL as chip_path_base,
    diffRun.state,
    -- data_state doesn't exist yet
    -- diffSkyfile.data_state,
    'full' AS data_state,
    diffSkyfile.magicked
FROM distRun
JOIN diffRun ON stage_id = diff_id
JOIN diffSkyfile using(diff_id)
JOIN rawExp using(exp_id)
LEFT JOIN distComponent 
    ON distRun.dist_id = distComponent.dist_id 
    AND diffSkyfile.skycell_id = distComponent.component
WHERE
    distRun.state = 'new'
    AND distRun.stage = 'diff'
    AND distComponent.dist_id IS NULL
UNION
SELECT DISTINCT
    distRun.dist_id,
    stage,
    stage_id,
    stackRun.skycell_id AS component,
    clean,
    rawExp.camera,
    outroot,
    stackSumSkyfile.path_base,
    NULL as chip_path_base,
    stackRun.state,
    -- data_state doesn't exist yet
    -- stackSumSkyfile.data_state,
    'full' AS data_state,
    0 AS magicked
FROM distRun
JOIN stackRun
    ON stage_id = stack_id
JOIN stackSumSkyfile
    USING(stack_id)
JOIN stackInputSkyfile
    USING(stack_id)
JOIN warpSkyfile
    ON  stackInputSkyfile.warp_id = warpSkyfile.warp_id
    AND stackRun.skycell_id       = warpSkyfile.skycell_id
    AND stackRun.tess_id          = warpSkyfile.tess_id
JOIN warpRun
    ON warpRun.warp_id = warpSkyfile.warp_id
JOIN fakeRun
    USING(fake_id)
JOIN camRun
    USING(cam_id)
JOIN chipRun
    ON camRun.chip_id = chipRun.chip_id
JOIN rawExp 
     USING (exp_id)
LEFT JOIN distComponent 
    ON distRun.dist_id = distComponent.dist_id 
    AND stackRun.skycell_id = distComponent.component
WHERE
    distRun.state = 'new'
    AND distRun.stage = 'stack'
    AND distComponent.dist_id IS NULL
) as Foo
