12. GROUP BY: One Row Per Group
12.4 How to Alias Aggregates Clearly for Reports
Why. Ugly aggregate headers confuse Excel exports and Power BI.
What. Alias every aggregate: SUM(amount_inr) AS sales_inr.
How. Name like a report column, not like a formula dump.
Steps
- Rewrite a GROUP BY with clear aliases.
- Prefer sales_inr over SUM(amount_inr).
- Keep units in the name.
Examples
Example 1 · dataset: raja_coffee
SELECT city AS store_city,
COUNT(*) AS order_count,
ROUND(SUM(amount_inr),2) AS sales_inr,
ROUND(AVG(amount_inr),2) AS avg_order_inr
FROM orders
GROUP BY city
ORDER BY sales_inr DESC;Output
| store_city | order_count | sales_inr | avg_order_inr |
|---|---|---|---|
| Pune | 10 | 9170.0 | 917.0 |
| Mumbai | 3 | 2490.0 | 830.0 |
| Kolhapur | 3 | 2410.0 | 803.33 |
| Nashik | 3 | 2320.0 | 773.33 |
| Goa | 3 | 1600.0 | 533.33 |
| Solapur | 2 | 730.0 | 365.0 |
Meaning. Report-ready headers.
Ravindra Bagale's Tip
Aliases travel into CSV exports — choose names managers understand.
Ravindra Bagale's Tip – मराठी
Aliases CSV exports मध्ये जातात — Managers समजतील अशी नावे.
Ravindra Bagale's Tip – हिंदी
Aliases CSV एक्सपोर्ट में जाते हैं — मैनेजर समझें ऐसे नाम चुनें।