Staggered-Rollout Panel and Weekly Rollup in SQL
SQL & DataModel answerLast updated
Write warehouse SQL that turns daily participation logs into a treatment-aware employee-day panel and a site-week participation table for a staggered-rollout DiD analysis.
Context
A company launched a commuter shuttle at some of its office sites on different dates. Analysts want to measure whether the shuttle raised office participation, using a staggered-adoption difference-in-differences design. Sites that never got the shuttle act as controls.
Schema
sites(site_id, city)
employees(employee_id, site_id, hire_date)
shuttle_service(site_id, start_date) -- rows exist only for sites that got the shuttle
participation(employee_id, date, participated)
Sample data:
| site_id | city |
|---|---|
| 1 | SEA |
| 2 | NYC |
| employee_id | site_id | hire_date |
|---|---|---|
| 101 | 1 | 2024-10-01 |
| 102 | 1 | 2025-01-01 |
| 201 | 2 | 2024-11-15 |
| site_id | start_date |
|---|---|
| 1 | 2025-01-15 |
| employee_id | date | participated |
|---|---|---|
| 101 | 2025-01-10 | 1 |
| 101 | 2025-01-20 | 1 |
| 102 | 2025-01-20 | 0 |
| 201 | 2025-01-10 | 1 |
| 201 | 2025-01-20 | 0 |
Task
Write SQL (PostgreSQL or BigQuery) that produces two outputs.
1. Employee-day panel with columns:
employee_id,site_id,date,participatedadoption_date: the site's shuttle start date, or NULL if the site never had onetreated_site: 1 ifdate >= adoption_dateat that site, otherwise 0. Never-treated sites always get 0.event_time_days:date - adoption_datein days, NULL for never-treated sitespost: a binary after-adoption indicator
2. Weekly site-level table with participation_rate = AVG(participated) for every site-week. Sites without a shuttle must be included and handled correctly.
Rules
- Be careful with the adoption boundary so there are no off-by-one errors.
- Employees never change sites, so the site in
employeesis valid for every day.
What to deliver
The SQL, the reasoning behind your join and boundary choices, and the output it should give on the sample data.
Hints
Hint 1
Start by reducing shuttle_service to a single adoption date per site, then decide which join type keeps sites that have no shuttle row.
Hint 2
Define the rule date >= adoption_date once and derive treated_site, post and event_time_days from it, so the columns cannot disagree. Check that both sides are DATE types, not timestamps.
Hint 3
Build the weekly table by grouping the finished employee-day panel rather than the raw tables. Think about what to do with the week that contains the adoption date, since it is part pre and part post.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The panel needs three things on every row: the outcome, the site's adoption date (NULL if never treated), and treatment flags that switch on exactly at adoption. The steps are:
- One adoption date per site. Aggregate
shuttle_servicewithMIN(start_date)per site. This is safe even if a site has duplicate or restarted service rows. - Attach site and adoption date. Join participation to
employees(inner join, since every participant should have a site) and then LEFT JOIN to the adoption CTE. The left join is the crucial step: an inner join would silently delete the never-treated sites, which are the control group. - Derive every treatment column from one boundary test,
date >= adoption_date. The adoption day is event time 0 and counts as treated. - Aggregate to site-week from the finished panel, so the weekly table inherits the same cleaning and boundary logic.
PostgreSQL
-- 1) Employee-day panel
CREATE TABLE did_panel AS
WITH site_start AS (
SELECT site_id, MIN(start_date::date) AS adoption_date
FROM shuttle_service
GROUP BY site_id
),
daily AS ( -- guard against duplicate employee-days
SELECT employee_id,
date::date AS date,
MAX(participated) AS participated
FROM participation
GROUP BY employee_id, date::date
),
joined AS (
SELECT d.employee_id,
e.site_id,
d.date,
d.participated,
s.adoption_date,
(s.adoption_date IS NOT NULL AND d.date >= s.adoption_date) AS is_post
FROM daily d
JOIN employees e ON e.employee_id = d.employee_id
LEFT JOIN site_start s ON s.site_id = e.site_id
)
SELECT employee_id, site_id, date, participated, adoption_date,
CASE WHEN is_post THEN 1 ELSE 0 END AS treated_site,
CASE WHEN adoption_date IS NULL THEN NULL
ELSE date - adoption_date END AS event_time_days,
CASE WHEN is_post THEN 1 ELSE 0 END AS post
FROM joined;
-- 2) Weekly site-level table
CREATE TABLE did_site_week AS
WITH w AS (
SELECT *, DATE_TRUNC('week', date)::date AS week_start
FROM did_panel
)
SELECT site_id,
week_start,
MAX(adoption_date) AS adoption_date,
COUNT(*) AS n_obs,
AVG(participated::numeric) AS participation_rate,
AVG(treated_site::numeric) AS treated_share,
CASE WHEN MAX(adoption_date) IS NOT NULL
AND week_start >= MAX(adoption_date)
THEN 1 ELSE 0 END AS fully_treated_week,
CASE WHEN MAX(adoption_date) IS NULL THEN NULL
ELSE (week_start - DATE_TRUNC('week', MAX(adoption_date))::date) / 7
END AS event_time_weeks
FROM w
GROUP BY site_id, week_start;
BigQuery changes
date - adoption_datebecomesDATE_DIFF(date, adoption_date, DAY).DATE_TRUNC('week', x)becomesDATE_TRUNC(x, WEEK(MONDAY)).x::datebecomesCAST(x AS DATE).- The boolean
is_postcan be used directly inCASE.AVGalready returns FLOAT64.
Why it is correct
Boundary handling.
- Treatment is defined once as
date >= adoption_date, and both sides areDATE. - Casting matters. If
start_datewere a timestamp such as2025-01-15 08:00, comparing it to the midnight date2025-01-15would wrongly call day 0 pre-treatment. - The other classic bugs are using
>instead of>=, or using an inclusive day-count helper that returns difference plus one. Plain date subtraction (orDATE_DIFF ... DAY) gives 0 on the adoption day.
Never-treated sites.
- The left join yields
adoption_date = NULL. - The
IS NOT NULLcheck insideis_postmakestreated_siteandpostequal 0 explicitly, rather than relying on NULL comparisons falling through toELSE. event_time_daysis NULL, as required.
post versus treated_site.
- When
postis defined relative to each site's own adoption date and set to 0 for never-treated sites, it is numerically identical totreated_site. - That is expected. In a staggered design the regressor is effectively
ever_treated × post. Keepingadoption_datelets analysts build cohort-based estimators such as Callaway–Sant'Anna or Sun–Abraham.
Weekly table.
- The week containing the adoption date is mixed: some days pre, some post. Rounding it either way would bias the estimate.
- So the table exposes
treated_share(fraction of treated observations),fully_treated_week, andevent_time_weeks(0 is the adoption week). Analysts can then drop or model the transition week. - Sites without a shuttle appear with
fully_treated_week = 0and NULL event time. participated::numericavoids integer-division surprises in engines that truncateAVGon integers.
Static sites. Because employees never move, one join to employees gives the correct site on every date. If they could move, you would need a site-history table joined on date ranges.
Expected output on the sample
Panel:
| employee_id | site_id | date | participated | adoption_date | treated_site | event_time_days | post |
|---|---|---|---|---|---|---|---|
| 101 | 1 | 2025-01-10 | 1 | 2025-01-15 | 0 | -5 | 0 |
| 101 | 1 | 2025-01-20 | 1 | 2025-01-15 | 1 | 5 | 1 |
| 102 | 1 | 2025-01-20 | 0 | 2025-01-15 | 1 | 5 | 1 |
| 201 | 2 | 2025-01-10 | 1 | NULL | 0 | NULL | 0 |
| 201 | 2 | 2025-01-20 | 0 | NULL | 0 | NULL | 0 |
Weekly:
| site_id | week_start | participation_rate | fully_treated_week | event_time_weeks |
|---|---|---|---|---|
| 1 | 2025-01-06 | 1.0 | 0 | -1 |
| 1 | 2025-01-20 | 0.5 | 1 | 1 |
| 2 | 2025-01-06 | 1.0 | 0 | NULL |
| 2 | 2025-01-20 | 0.0 | 0 | NULL |
Site 1 has no row for the adoption week starting 2025-01-13 because there is no participation data in that week. If a balanced panel is needed, cross join sites with a generated week calendar and left join the aggregates.
Common mistakes
- Inner joining to the adoption table and losing the control sites.
- Comparing a timestamp to a date, or using
>at the boundary. - Computing weekly treatment from raw tables with a different rule than the daily panel.
- Averaging per-employee rates instead of averaging all employee-day observations, which changes the weighting.
- Forgetting that
MIN(start_date)is needed when a site has several service rows.
Optionally, if the data can contain days before an employee's hire_date, add WHERE d.date >= e.hire_date to the joined CTE. The spec does not require it, so state it as an assumption.
Complexity
With P participation rows, the work is O(P) using hash aggregates and joins, or O(P log P) with sort-based plans. The employees, sites and shuttle_service tables are small. The panel takes O(P) space, and the weekly table takes O(sites × weeks).