A `select` statement which aggregates has to say what it aggregates over. Every selected column is either aggregated with a [[Invantive UniversalSQL/General/Group Functions|group function]] or named in the `group by` clause. A column which is neither is refused with: ``` itgensql037 At least one field did not appear in the group by nor was it aggregated: … ``` ## Grouping on a Column Alias The `group by` clause accepts the alias of a selected column, not only the expression behind it. So this is valid: ```sql select division_code || '-' || account_code as reference , sum(amount_dc) as total from transaction_lines group by reference ``` Repeating the whole expression in `group by` is therefore unnecessary, and so is wrapping the query in an inline view purely to make the alias available: ```sql select reference , sum(amount_dc) from ( select division_code || '-' || account_code as reference , amount_dc from transaction_lines ) group by reference ``` Both forms give the same answer. The first is shorter, and an expression written once cannot drift from a copy of itself. ## Which Release Grouping on an alias is available from release 24.1. A release before that refuses the first form with `itgensql037`, which is why older statements are often written as the second form. Such a statement keeps working; it does not have to be rewritten. ## Where the Alias Is Not Enough An alias resolves to the expression it names, so grouping on an alias groups on that expression. Where two selected columns carry the same alias, or where an alias has the same name as a column of the source, name the expression rather than the alias to leave no doubt about which is meant.