Det er ikke kønt, da det skal forbinde fire kopier af dit bord til sig selv, hvilket kan ramme alle former for præstationssmerter (jeg stærkt råde dig til at have indekser på id
og date
)... men dette vil gøre tricket:
SELECT y.report_date, SUM(x.value)
FROM mytable AS x
NATURAL JOIN (
SELECT a.id, b.date AS report_date, MAX(c.date) AS date
FROM (SELECT DISTINCT id FROM mytable) a JOIN
(SELECT DISTINCT date FROM mytable) b JOIN
mytable AS c ON (c.id = a.id AND c.date <= b.date)
GROUP BY a.id, b.date
) AS y
GROUP BY y.report_date
Se den på sqlfiddle .