Newer Version Available
Use Aliases with GROUP BY
Specify the alias directly after the associated field. For example, the following query contains two aliases: n for the Name field, and max for the MAX(Amount) aggregated field.
1SELECT Name n, MAX(Amount) max
2FROM Opportunity
3GROUP BY NameAny aggregated field in a SELECT list that does not have an alias automatically gets an implied alias with a format expri, where i denotes the order of the aggregated fields with no explicit aliases. The value of i starts at 0 and increments for every aggregated field with no explicit alias.
In this example, MAX(Amount) has an implied alias of expr0, and MIN(Amount) has an implied alias of expr1.
1SELECT Name, MAX(Amount), MIN(Amount)
2FROM Opportunity
3GROUP BY NameIn this query, MIN(Amount) has an explicit alias of min, MAX(Amount) has an implied alias of expr0, and SUM(Amount) has an implied alias of expr1.
1SELECT Name, MAX(Amount), MIN(Amount) min, SUM(Amount)
2FROM Opportunity
3GROUP BY Name