TIL: How to optimize Group by queries in SQL Server

from blog Just Some Code, | ↗ original
Let me share this technique I learned to improve queries with GROUP BY in SQL Server. To improve queries with GROUP BY, write the SELECT query with the GROUP BY part using only the needed columns to do the grouping or sorting inside a common table expression (CTE) first. Then, join the CTE with the right tables to find other columns. Usual GROUP...