SELECT DISTINCT
    distRun.dist_id,
    distRun.label,
    distTarget.dist_group,
    stage,
    stage_id,
    diffSkyfile.skycell_id AS component,
    clean,
    rawExp.camera,
    CONCAT_WS('.', outroot, CONVERT(distRun.dist_id, CHAR)) as outdir,
    diffSkyfile.path_base,
    CAST(NULL AS CHAR(255)) as chip_path_base,
    diffRun.state,
    -- data_state doesn't exist yet
    -- diffSkyfile.data_state,
    'full' AS data_state,
    diffSkyfile.quality,
    distRun.no_magic,
    0 AS magicked
FROM distRun
JOIN distTarget USING(target_id, stage, clean)
JOIN diffRun ON stage_id = diff_id
JOIN diffSkyfile using(diff_id)
JOIN diffInputSkyfile USING(diff_id, skycell_id)
JOIN stackRun
    ON stackRun.stack_id = diffInputSkyfile.stack1 
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 USING(chip_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 = 'SSdiff'
    AND distComponent.dist_id IS NULL
    AND ((diffRun.magicked > 0) OR distRun.no_magic)
    AND (diffRun.state = 'full' OR (distRun.clean AND diffRun.state = 'cleaned'))
