- How do I calculate a moving average (rolling average) in SQL?
- Ask in plain English — '7-day moving average of daily signups.' nlqdb compiles the window function with a frame, `AVG(signups) OVER (ORDER BY day ROWS BETWEEN 6 PRECEDING AND CURRENT ROW)`, runs it in Postgres, and returns the smoothed rows plus the SQL it ran. You get the rolling curve without hand-writing the frame clause. The honest limit: you name the window size and the order it slides over.
- What's the difference between a moving average and a running total?
- A running total accumulates every row from the start, so the sum only grows. A moving average averages a fixed window of recent rows and slides it forward, so old rows drop out — `AVG(...) OVER (ORDER BY day ROWS BETWEEN 6 PRECEDING AND CURRENT ROW)`. nlqdb picks the frame from whether you ask for a 'moving' or 'rolling' average or a 'running' or 'cumulative' total, and shows the compiled SQL either way.
- How do I change the window size — a 30-day or 4-week moving average?
- Name the window in your ask: 'rolling 30-day average' compiles `ROWS BETWEEN 29 PRECEDING AND CURRENT ROW`; a '4-week moving average' over weekly rows compiles `ROWS BETWEEN 3 PRECEDING AND CURRENT ROW`. The frame counts rows, so the count is one less than the window length — a 7-day window is 6 preceding plus the current row. nlqdb recompiles the frame when you restate the window and shows the SQL.
- Should I use ROWS or RANGE, and what about gaps in the dates?
- `ROWS` counts physical rows; `RANGE` counts by the ordering value. On a dense daily series they agree, but if some days have no row, `ROWS BETWEEN 6 PRECEDING` spans seven rows that skip the gaps — not seven calendar days. Fill the gaps first so every day is a row (see counting rows per day, including missing dates), then the row-based window is a true 7-day average.
- Does the moving-average query work in Snowflake, BigQuery, or MySQL?
- The pattern is ANSI-standard: `AVG(x) OVER (ORDER BY day ROWS BETWEEN 6 PRECEDING AND CURRENT ROW)` runs unchanged on Postgres, Snowflake, BigQuery, and MySQL 8+. Older MySQL (< 8.0) has no window functions, so a moving average needs a self-join or correlated subquery instead. nlqdb runs your ask on Postgres today and shows the compiled SQL — the same frame clause you'd paste into any modern warehouse.