Aggregate Functions as Window Functions: Functions
Module: Window Functions
**Basic Syntax Pattern:**
```sql
SELECT
detail_columns,
AGGREGATE_FUNCTION(column) OVER (
[PARTITION BY partition_columns]
[ORDER BY order_columns]
[ROWS/RANGE frame_specification]
) AS aggregate_alias
FROM table_name;
```
**Common Aggregate Functions:**
```sql
-- SUM: Running totals and percentages
SUM(amount) OVER() -- Grand total
SUM(amount) OVER(PARTITION BY category) -- Category totals
SUM(amount) OVER(ORDER BY date) -- Running total
-- COUNT: Row counts and running counts
COUNT(*) OVER() -- Total row count
COUNT(*) OVER(PARTITION BY group_col) -- Group counts
COUNT(*) OVER(ORDER BY date) -- Running count
-- AVG: Averages for comparison
AVG(value) OVER() -- Overall average
AVG(value) OVER(PARTITION BY category) -- Group averages
AVG(value) OVER(ORDER BY date ROWS 6 PRECEDING) -- Moving average
-- MIN/MAX: Boundary values
MIN(value) OVER() -- Overall minimum
MAX(value) OVER(PARTITION BY group_col) -- Group maximum
```
**Practical Examples:**
```sql
-- 1. Sales dashboard with percentages
SELECT
salesperson,
region,
sales_amount,
SUM(sales_amount) OVER() as total_sales,
ROUND(sales_amount * 100.0 / SUM(sales_amount) OVER(), 2) as pct_of_total
FROM sales;
-- 2. Running totals by date