Aller au contenu principal

Add a window function without collapsing rows

Use a window function when you need group context while keeping row detail.

sql-fundamentals5 min read

Show each deal with the reps average deal size without losing individual deal rows. Use GROUP BY to collapse rows; use a window function to add group context to rows. Using GROUP BY rep_id answers one row per rep, not one row per deal. 1 SELECT deal_id, rep_id, amount Keep the row-level fields needed for coaching. 2 AVG(amount) OVER (PARTITION BY rep_id) AS rep_avg_amount Compute the average across each reps partition while returning it on every deal row. 3 amount - rep_avg_amount AS delta_to_rep_avg Compare the current row to its group context in the final output. Every deal remains visible,…

Read the full lesson

Sign up free: one personalized lesson every day, matched to your role and goals.

← Back to library

Nous contacter