Running totals, rankings and month-over-month change in SQL
Prompt that writes and explains SQL with window functions for running totals, ranks and comparisons. Paste it into an AI tool of your choice, which then writes the SQL query.
- Topic
- Spreadsheets & data
- Type
- Prompt
- Level
- Intermediate
- Time
- about 10 min
Results depend on the tool, the model and what you enter.
Result preview
Example: written by our editors, not real AI output.
SUM(amount) OVER ( PARTITION BY region ORDER BY month ) AS revenue_to_date Explanation: adds up all months up to the current one, per region. Check: last value per region = SUM(amount) per region Assumption: table orders(region, month, amount)
Fill in
What you type stays in this browser window. We store and send nothing.
- running revenue year to date
- top three products per region
- percent change versus the previous month
- seven-day moving average
- first and last purchase per customer
- orders with date and amount
- website visits per day
- tickets with status
- stock movements
- subscriptions with start and end
- PostgreSQL
- MySQL
- SQL Server
- BigQuery
- SQLite
Your prompt
You have edited the prompt. Changes in the fields above no longer overwrite it.
This is an instruction for an AI, not the result itself.
Where this prompt works
These AI tools are a good fit for this prompt:
How to use it
- Fill in the fields or tap a suggestion. Leave out names, addresses and other personal data.
- Copy the finished prompt into an AI tool of your choice.
- Compare the last value of the running total with a simple sum over the same period. Both must match.
Why does this work?
Window functions save helper tables and spreadsheet formulas, but without clear ordering and partitioning they quietly return wrong numbers. Explained queries with a check query make the result traceable.
Tips
- Enter only table and column names and made-up sample rows, never real customer data.