Advanced Analytics Functions: Functions
Module: Window Functions
**Core Statistical Functions:**
-- Standard deviation and variance
SELECT
department,
AVG(salary) as avg_salary,
STDDEV(salary) as salary_stddev,
VARIANCE(salary) as salary_variance
FROM employees
GROUP BY department;
-- Correlation between two variables
SELECT
CORR(years_experience, salary) as experience_salary_correlation
FROM employees;
**Distribution Functions:**
-- Percentile rank (0 to 1)
SELECT
name,
salary,
PERCENT_RANK() OVER (ORDER BY salary) as percentile_rank
FROM employees;
-- Cumulative distribution
SELECT
name,
salary,
CUME_DIST() OVER (ORDER BY salary) as cumulative_dist
FROM employees;
-- Specific percentile values
SELECT
PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY salary) as median,
PERCENTILE_CONT(0.75) WITHIN GROUP (ORDER BY salary) as q3,
PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY salary) as p90
FROM employees;
**Advanced Patterns:**
-- Multiple functions with window frames
SELECT
date,
sales,
STDDEV(sales) OVER (
ORDER BY date
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW