RANGE BETWEEN (Date Range)
RANGE BETWEEN (Date Range) is a SQL function in the Window Functions category. PostgreSQL: window frame based on date ranges rather than row counts. Perfect for time-series data. The syntax is SUM(value) OVER (ORDER BY date RANGE BETWEEN INTERVAL '7' DAY PRECEDING AND CURRENT ROW). It accepts value. It returns date-range window result. A typical example: SELECT sale_date, daily_revenue, SUM(daily_revenue) OVER ( ORDER BY sale_date RANGE BETWEEN INTERVAL '7' DAY PRECEDING AND CURRENT ROW ) AS trailing_7day_total FROM daily_sales; -- Unlike ROWS (which counts rows), RANGE considers actual date gaps -- If data is missing for a day, ROWS would include wrong number of real days A close relative is ROW_NUMBER(), which assigns a unique sequential integer to each row within its partition (1, 2, 3...). A close relative is RANK(), which similar to ROW_NUMBER but gives the same rank to equal values. Leaves gaps in ranking.