Window Frames (ROWS vs RANGE): Functions
Module: Window Functions
**ROWS Frame Syntax:**
```sql
-- Physical row counting
FUNCTION() OVER (
ORDER BY columns
ROWS BETWEEN start_boundary AND end_boundary
)
```
**RANGE Frame Syntax:**
```sql
-- Value-based grouping
FUNCTION() OVER (
ORDER BY columns
RANGE BETWEEN start_boundary AND end_boundary
)
```
**Time-based RANGE (PostgreSQL):**
```sql
-- Time interval ranges
FUNCTION() OVER (
ORDER BY timestamp_column
RANGE BETWEEN INTERVAL 'X units' PRECEDING AND CURRENT ROW
)
```
**Common Patterns:**
```sql
-- Moving average (ROWS)
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW -- Exactly 7 rows
-- Cumulative sum (RANGE)
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW -- All tied values
-- Same-day analysis (RANGE)
RANGE BETWEEN CURRENT ROW AND CURRENT ROW -- All rows with same ORDER BY value
```
ROWS counts physical database rows in their storage/processing order
RANGE groups rows by ORDER BY column values, including all tied values
ROWS gives predictable, exact counts regardless of data values
RANGE can include more or fewer rows than specified due to tie handling
Both require ORDER BY clause to define the sorting for frame boundaries
RANGE supports time intervals (INTERVAL) in PostgreSQL for temporal analysis
Full support for both ROWS and RANGE. RANGE supports INTERVAL for time-based frames.