The source report identified a hard SQL question about a rolling sum but did not preserve its schema or exact output. The following is a self-contained practice reconstruction of that topic.
You have a PostgreSQL table:
orders
------
order_id BIGINT PRIMARY KEY
seller_id BIGINT NOT NULL
ordered_at TIMESTAMPTZ NOT NULL
status TEXT NOT NULL -- 'completed', 'cancelled', or 'refunded'
amount NUMERIC NOT NULL -- nonnegative order amount
Write one PostgreSQL query that returns a daily revenue series for every seller with at least one completed order. For each such seller:
-
Start the series on that seller's earliest UTC completed-order date.
-
End the series on that seller's latest UTC completed-order date.
-
Include every calendar date in between, even when the seller has no completed orders that day.
-
daily_revenue
is the sum of
amount
for completed orders on that UTC date; use
0
on a missing date.
-
rolling_7d_revenue
is the sum of
daily_revenue
for the current calendar date and the previous six calendar dates. Before a seller has seven dates of history, use all dates available since that seller's series began.
-
Cancelled and refunded orders contribute nothing.
Return exactly these columns:
-
order_date
-
seller_id
-
daily_revenue
-
rolling_7d_revenue
The window is based on calendar dates, not the previous seven order rows. Preserve numeric precision and order the final result by seller_id ascending and order_date ascending.