SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE recibo ( `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, `activation_date` DATETIME, `el_value` int(11), `unPy` char ); INSERT INTO recibo (id, activation_date, el_value, unPy) VALUES (1, '2020-01-15', 10, 'a' ), (2, '2020-02-13', 10, 'a' ), (3, '2020-02-15', 10, 'a' ), (31, '2020-02-15', 20, 'b' ), (41, '2020-02-15', 30, 'b' ), (4, '2020-03-01', 10, 'a' ), (5, '2020-03-03', 10, 'a' ), (6, '2020-05-01', 10, 'b' ), (7, '2020-06-01', 10, 'a' ), (8, '2020-07-15', 10, 'b' ), (9, '2020-08-15', 10, 'a' ), (10, '2020-08-15', 10, 'a' ), (11, '2020-08-19', 10, 'b' ); SELECT id, activation_date, py, date_format(activation_date, "%Y-%m") as anyoMes, el_value, SUM(el_value) OVER(ORDER BY activation_date) AS cumulative_sum FROM recibo ; select laUnion.anyoMes, max(laUnion.cumulative_sum) as acumulado from( SELECT id, unPy, activation_date, date_format(activation_date, "%Y-%m") as anyoMes, el_value, SUM(el_value) OVER(partition by py ORDER BY activation_date) AS cumulative_sum FROM recibo ) as laUnion group by laUnion.anyoMes -- group by anyoMes ;
Stuck with a problem? Got Error? Ask ChatGPT!
Copy Clear