How to apply a SUM operation without grouping the results in SQL? How to apply a SUM operation without grouping the results in SQL? sql sql

How to apply a SUM operation without grouping the results in SQL?


IT would depend on your SQL server, in Postgres/Oracle I'd use Window Functions. In MySQL... not possible afaik.

Perhaps you can fake it like this:

SELECT a.id, SUM(b.value) AS `sum`FROM test AS aJOIN test AS b ON a.`group` = b.`group`GROUP BY a.id, b.`group`;


No there isn't AFAIK. You will have to use a join like

SELECT t.`id`, tsum.sum AS `sum`FROM `test` as t GROUP BY `group`JOIN (SELECT `id`, SUM(`value`) AS `sum` FROM `test` GROUP BY `group`) AS tsum     ON tsum.id = t.id