Q:

Sum of revenue everyday, for last 30 days on each day

I have a simple table.
 Date | Revenue
 5/1    12
 5/2    25
 5/3    93
 .
 .
 11/15  47

I am trying to write a query that returns two columns. The first column is Date, day-by-day, like the original table. The second column is 30-Day-Revenue, which is the sum of the "Revenue" column in the original table for the last 30 days, ending on the displayed date. There is a lot of overlap when we sum. Thanks in advance!

A:

I have an alternative solution (assumes your table is called revenue_table):
SELECT a.Date, SUM(b.Revenue)
FROM revenue_table a, revenue_table b
WHERE b.Date <= a.Date AND b.Date > a.Date - 30
GROUP BY a.Date;

