invantive documentation
Invantive Support ForumsLog In / Sign Up

Grouping

A select statement which aggregates has to say what it aggregates over. Every selected column is either aggregated with a 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:

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:

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.

Filtering Groups with Having

The having clause keeps the groups for which its condition is true, as the where clause keeps rows. It is evaluated after the grouping, so it can use the result of a group function:

select division_code
,      count(*)       as lines
,      sum(amount_dc) as total
from   transaction_lines
group
by     division_code
having count(*) > 100
and    sum(amount_dc) > 10000

A group function can be named once and used by its alias in having; the example below gives the same groups:

select division_code
,      count(*)       as lines
,      sum(amount_dc) as total
from   transaction_lines
group
by     division_code
having lines > 100
and    total > 10000

A group function that only having uses is computed as well, and is not a column of the result. Without a group by all rows form one group, also when there are none: select count(*) from transaction_lines where 1 = 0 having count(*) = 0 answers one row.

Outside a group function, having can use a column the statement groups by and the alias of a selected column that is no group function. Any other column is refused, as it has no single value per group:

itgensql954
The having clause uses the column '…', which is neither in the group by nor inside a group function.

Without a group by every column must be inside a group function (itgensql955). A name that is both a column of the source and the alias of a group function is refused with itgensql956, rather than resolved to either: give the group function another alias, or qualify the column. A subquery in having can not refer to the query around it (itgensql957). A group function inside an expression is only possible in having: elsewhere, such as in where sum(amount_dc) > 0, it is refused with itgensql953.

A condition in having on a column the statement groups by gives the same answer as the same condition in where. From release 28.0 such a condition on a number, a boolean or a GUID is evaluated before the grouping, as a where clause is, and can be handed to the platform. A condition on a text or a date stays after the grouping: one group can hold texts that differ only in leading or trailing blanks, and dates that differ within a second, which the condition could tell apart. Written in where, the condition filters the rows before the grouping in every case.

A top or limit after having limits the rows answered, not the work: a group is complete only when all rows are read.

The word having can still be used as the name of a column or an alias.

Memory Used by Grouping

From release 28.0 a grouping keeps one row per group instead of all rows, when its group functions are count, sum, min, max, avg, first, last and stddev, with or without distinct. The distinct variants keep the distinct values of each group, and stddev keeps the values of each group. A grouping with listagg, zip or product, or with avg over a column of single precision floating point, still holds all rows until the last one is read. The answers are the same either way.

Having Before Release 28.0

having is available from release 28.0. Before that, the same result is had with an inline view, which keeps working:

select division_code
,      lines
from   ( select division_code
         ,      count(*) as lines
         from   transaction_lines
         group
         by     division_code
       )
where  lines > 100