Data Analysis & SQL5.0 · 0 ratings

Time-Series Aggregation With Gap Filling

Produces a continuous, gap-filled time series with zeros for empty buckets and correct period boundaries.

Role-BasedStep-by-StepStructured-Output

Prompt

ROLE: You are an analyst who builds dense time-series reports that never silently drop empty periods.

CONTEXT: I need [METRIC] aggregated by [GRAIN] (hour/day/week/month) over [DATE_RANGE], optionally split by [DIMENSION]. Source table: [SCHEMA]. Engine: [DATABASE_ENGINE]. Timezone: [TIMEZONE].

TASK:
1. Generate a complete spine of every period in the range using a date/number generator appropriate to [DATABASE_ENGINE] (generate_series / GENERATE_DATE_ARRAY / recursive CTE).
2. If a dimension split is requested, cross join the spine with the distinct dimension values so every (period, dimension) cell exists.
3. LEFT JOIN the aggregated data onto the spine and COALESCE missing measures to 0.
4. Truncate timestamps to the period boundary correctly for the given timezone, and make range bounds explicitly inclusive/exclusive.
5. Add a 7-period moving average column as an example trend smoother.

OUTPUT FORMAT: Approach notes -> Final ```sql``` (spine -> aggregate -> join -> moving average) -> Boundary & timezone notes.

CONSTRAINTS: Empty buckets must appear as 0, not be absent. Pin the timezone before truncation. Keep range bounds half-open [start, end). Do not let the LEFT JOIN turn into an INNER join via a WHERE filter on the right table.

How to use this prompt

  1. 1

    Copy the prompt above and paste it into ChatGPT, Claude, or Gemini — or open it in the visual Studio to edit each part on a canvas and run it with your own key.

  2. 2

    Replace any bracketed placeholders with your specifics. The more concrete your context and constraints, the sharper the result — see the 5-part prompt structure.

  3. 3

    Run it, then refine. Ask the model to critique and improve its own answer with self-critique prompting.

Techniques in this prompt

Role-Based

Assigns the model an expert persona so it adopts the right vocabulary, depth, and standards for the task.

Learn this technique
Step-by-Step

Forces explicit intermediate reasoning instead of jumping to a conclusion, which improves accuracy on hard tasks.

Learn this technique
Structured Output

Pins the response to a defined structure so it drops straight into your workflow.

Learn this technique

Recommended models

claudegpt-4ogemini

Build on this prompt

Open it in the visual Studio to wire it into a full workflow with your own API key — or learn the craft behind prompts like this.

More in Data Analysis & SQL