Help with recursing self-referencing table
Start with the recursive CTE to get the tree. Keep track of the parent too (faking the bottom level), to identify when it's null.

WITH Tree AS (
    SELECT Id AS VariableId, DataSourceId, DataSourceId AS Parent
    FROM Variable
    UNION ALL
    SELECT t.VariableId, t.DataSourceId, d.ParentId
    FROM Tree t
    JOIN DataSource d
    ON d.Id = t.Parent
)
--Now get the top levels
SELECT VariableId, DataSourceId
FROM Tree
WHERE Parent IS NULL;