How to SUM DISTINCT by other columns?
Nevermind, found a way, by adding one more CTE
WITH cte AS ( ... )
, sum1 AS (
  SELECT i, MAX(x) tot -- or AVERAGE/MIN
  FROM cte
  GROUP BY 1
)
SELECT ...
   , (SELECT SUM(tot) FROM sum1) AS sum1
FROM cte