How to add the aliases to the group by clause
Using the with clause will help keep your queries readable:

with &lt;some_meaningful_name&gt; as (
  select
    &lt;complex_expression1&gt; as &lt;alias1&gt;,
    &lt;complex_expression2&gt; as &lt;alias2&gt;,
    &lt;other_columns&gt;
  from
    ...
)
select
  &lt;some_columns&gt;,
  &lt;aggregate_expression&gt;
from
  some_meaningful_name
group by
  &lt;some_columns&gt;;