Problems / Stores Beating Company Average / Editorial
AVG(revenue) GROUP BY month
company_avg
revenue >= 1.2 * company_avg
"Compare each row to its group's aggregate" is the two-grain pattern: aggregate at the coarse grain, rejoin to the fine grain, compare. It appears as a CTE+join here, as a correlated subquery in older codebases, and as AVG(...) OVER (PARTITION BY ...) in window form — all the same idea with different ergonomics. The discipline that matters across all three: keep the benchmark *exact* until the final projection. A benchmark rounded before the comparison turns your carefully specified threshold into a slightly different one.
AVG(...) OVER (PARTITION BY ...)
Solve Stores Beating Company Average yourself →