SELECT
    id,
    madelines_files_results.size AS stored_size,
    d.decrypted,
    COALESCE(madelines_files_results.size, d.size) AS calculated_size
FROM dtf.madelines_files_results
LEFT JOIN
(
    SELECT decrypted,AVG(size)::bigint AS size FROM dtf.madelines_files_results
    WHERE parent_id IS NOT NULL
    GROUP BY decrypted
) AS d ON madelines_files_results.decrypted = d.decrypted
WHERE parent_id IS NOT NULL
ORDER BY madelines_files_results.id
