КТ
SELECT *
FROM tmp444
┌─day─┬─user─┬─cost─┐
│ 1 │ me │ 2 │
│ 2 │ me │ 4 │
│ 1 │ you │ 1 │
│ 2 │ you │ 3 │
└─────┴──────┴──────┘
WITH t AS
(
SELECT
day,
user,
sum(cost) AS sum
FROM tmp444
GROUP BY
day,
user
)
SELECT *
FROM t
INNER JOIN
(
SELECT
user,
sum(sum) AS total
FROM t
GROUP BY user
) AS t2 USING (user)
┌─day─┬─user─┬─sum─┬─total─┐
│ 2 │ me │ 4 │ 6 │
│ 1 │ you │ 1 │ 4 │
│ 2 │ you │ 3 │ 4 │
│ 1 │ me │ 2 │ 6 │
└─────┴──────┴─────┴───────┘