Monday, September 28, 2009

Computes the cumulative running sum

CREATE TABLE demo (
submit_date DATE NOT NULL,
num_votes NUMBER NOT NULL);

INSERT INTO demo VALUES (TRUNC(SYSDATE)-4, 100);
INSERT INTO demo VALUES (TRUNC(SYSDATE)-3, 150);
INSERT INTO demo VALUES (TRUNC(SYSDATE)-2, 75);
INSERT INTO demo VALUES (TRUNC(SYSDATE)-3, 25);
INSERT INTO demo VALUES (TRUNC(SYSDATE)-1, 50);
COMMIT;

SELECT * FROM demo;

SELECT submit_date, num_votes, SUM(num_votes)
OVER(ORDER BY submit_date ROWS UNBOUNDED PRECEDING) TOT_VOTE
FROM demo
ORDER BY submit_date;

No comments: