add batch remove batch split batch comment selection show hidden batches hide batch highlight batch
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE test (
bucket int(10) NOT NULL,
label varchar(10) NOT NULL,
amount int(10) NOT NULL
);

INSERT INTO test VALUES
(1, "A", 1),
(1, "B", 2),
(1, "C", 3),
(2, "A", 4),
(2, "B", 5),
(2, "C", 6),
(3, "A", 7),
(3, "B", 8),
(3, "C", 9),
(4, "A", 10),
(4, "B", 11),
(4, "C", 12);
SELECT t.*,
SUM(amount) OVER (PARTITION BY label
ORDER BY amount
) AS running_total
FROM test t
ORDER BY bucket, amount;
bucket label amount running_total
1 A 1 1
1 B 2 2
1 C 3 3
2 A 4 5
2 B 5 7
2 C 6 9
3 A 7 12
3 B 8 15
3 C 9 18
4 A 10 22
4 B 11 26
4 C 12 30
SELECT t.*,
SUM(amount) OVER (PARTITION BY label
ORDER BY bucket, amount
) AS running_total
FROM test t
ORDER BY bucket, amount;
bucket label amount running_total
1 A 1 1
1 B 2 2
1 C 3 3
2 A 4 5
2 B 5 7
2 C 6 9
3 A 7 12
3 B 8 15
3 C 9 18
4 A 10 22
4 B 11 26
4 C 12 30