The situation
A dashboard draws daily revenue per store as a line chart. The chart library connects the points it is given. When a store has no sales on a day, there is no point for that day, and the line runs straight from the day before to the day after. A closed store looks like a normal day.
The chart needs a row for every store and every day, with 0 where nothing was sold.
Input tables
stores
One row per store.
| Column | Type | Description |
|---|---|---|
store_id |
BIGINT | Unique per store. |
store_name |
STRING | Display name. |
calendar
One row per calendar day, from 1 January 2025 to 30 June 2025.
| Column | Type | Description |
|---|---|---|
cal_date |
DATE | The day. |
is_weekend |
BOOLEAN | True on Saturdays and Sundays. |
sales
One row per sale. A store can have several sales on a day, or none.
| Column | Type | Description |
|---|---|---|
sale_id |
BIGINT | Unique per sale. |
store_id |
BIGINT | The store. |
sale_date |
DATE | Date of the sale. |
amount |
DECIMAL(10,2) | Sale value in rupees. |
Requirement
Return daily revenue for March 2025.
- One row for every store in
storesand every day from 1 March 2025 to 31 March 2025, both included. That is 31 rows per store. revenueis the sum of the store's sales on that day, and 0.00 if it had none.- A store with no sales in March at all still gets its 31 rows.
- Sales outside March do not appear.
Expected output
One row per store per day of March.
| Column | Type | Description |
|---|---|---|
store_id |
BIGINT | |
sale_date |
DATE | A day of March 2025. |
revenue |
DECIMAL(12,2) | 0.00 on days without sales. |
Solve it in the browser
This editor runs DuckDB inside your browser. Nothing is installed and nothing is sent to a server. DuckDB's SQL is close to Spark SQL but not identical. The Colab notebook runs real Spark.
Your result appears here. Your draft is kept in this browser.
Explanation
Show explanationGives the answer away. Try the problem first.
The approach
GROUP BY store_id, sale_date produces one row for every combination that exists in
sales. A day without sales has no rows, so there is nothing to group and nothing to
output. No amount of COALESCE helps: COALESCE replaces a NULL value in a row that
exists. It cannot create a row.
The rows have to come from somewhere else. The complete list of (store, day) pairs is built from two tables that do have every store and every day:
FROM stores s
CROSS JOIN calendar c
WHERE c.cal_date >= DATE '2025-03-01' AND c.cal_date < DATE '2025-04-01'
Six stores times 31 days is 186 rows. This list is sometimes called a date spine. Then the sales are attached to it with a left join, and the missing ones become 0:
LEFT JOIN daily d
ON d.store_id = s.store_id
AND d.sale_date = c.cal_date
...
COALESCE(d.revenue, 0)
daily is the sales summed per store and day beforehand. Aggregating before the join
keeps one row per (store, day) on both sides, so the join cannot multiply anything.
Of the 186 rows, 77 are days without sales.
Common wrong answers
GROUP BY on sales alone. Returns 109 rows: only the days that have sales. This
is the chart with the misleading straight lines.
Taking the list of stores from sales. Returns 155 rows. Store 6 had no sales in
March, so a list of stores built from March sales does not contain it, and all 31 of
its rows are missing. A store that sold nothing all month is exactly what the
dashboard should show.
Joining raw sales to the spine without aggregating. Returns 311 rows. A day with
three sales becomes three rows. Either aggregate first, as above, or GROUP BY the
spine columns after the join.
Filtering the sales date in WHERE. Something like
WHERE x.sale_date >= DATE '2025-03-01' after the left join removes every spine row
that found no sale, because its sale_date is NULL. Back to 109 rows. The date range
belongs on the calendar, which is the left side. (Problem 13 is about this mistake.)
Forgetting the calendar filter. Returns 1,086 rows: every store for every day of the six-month calendar.
No COALESCE. 186 rows, with NULL instead of 0.00 on the 77 empty days. Many
chart libraries skip NULL points, which brings the original problem back.
Where the calendar comes from
Here it is a table, as it is in most warehouses: a date dimension is one of the first tables anyone builds. Without one, Spark can generate the days:
SELECT explode(sequence(DATE '2025-03-01', DATE '2025-03-31', INTERVAL 1 DAY)) AS cal_date
Edge cases in the data
| Case | Where | What it catches |
|---|---|---|
| Days without sales | 77 of 186 store-days | GROUP BY on sales alone. |
| A store with no sales in March | store 6 | Building the store list from sales. |
| Several sales on one day | most open days | Joining before aggregating. |
| Sales on 28 February and 1 April | store 1 | A date range that is one day too wide. |
| Sales on 1 March and 31 March | store 1 | A date range that is one day too narrow. |
| Calendar covers January to June | calendar |
Forgetting to restrict the spine. |