← aggr operator page · clause/aggr/having/manual_ex_3
pandas ✅ · DuckDB ✅
aggr Me_1 := sum( Me_1 ), Me_2 := max( Me_1) group by Id_1 , Id_2 with having avg (Me_1 ) > 2.
Aggregation, Example 3, with the manual's data points: "only groups for which havingCondition evaluates to true appear in the result", and the condition "must invoke aggregate operators" (avg here, where the manual and the reading of #1119 agree).
(1,B) and (2,A), whose averages are 4 and 4.5, are kept with the sums 8 and 9 and the maxima 5 and 7; (1,A) averages 1. Me_1 may be Integer, as the manual writes it, or Number, by the integer rule of Sum, and Me_2 stays an Integer, as for max "For numeric operands, the result has the same type as the operand".
DS_r := DS_1 [ aggr Me_1:= sum( Me_1 ), Me_2 := max( Me_1) group by Id_1 , Id_2 having avg (Me_1 ) > 2 ];
| Id_1 | Id_2 | Id_3 | Me_1 |
|---|---|---|---|
| 1 | A | XX | 0 |
| 1 | A | YY | 2 |
| 1 | B | XX | 3 |
| 1 | B | YY | 5 |
| 2 | A | XX | 7 |
| 2 | A | YY | 2 |
{
"datasets": [
{
"name": "DS_1",
"DataStructure": [
{"name": "Id_1", "type": "Integer", "role": "Identifier", "nullable": false},
{"name": "Id_2", "type": "String", "role": "Identifier", "nullable": false},
{"name": "Id_3", "type": "String", "role": "Identifier", "nullable": false},
{"name": "Me_1", "type": "Integer", "role": "Measure", "nullable": true}
]
}
]
}
| Id_1 | Id_2 | Me_1 | Me_2 |
|---|---|---|---|
| 1 | B | 8 | 5 |
| 2 | A | 9 | 7 |
{
"datasets": [
{
"name": "DS_r",
"DataStructure": [
{"name": "Id_1", "type": "Integer", "role": "Identifier"},
{"name": "Id_2", "type": "String", "role": "Identifier"},
{"name": "Me_1", "type": ["Integer", "Number"], "role": "Measure"},
{"name": "Me_2", "type": "Integer", "role": "Measure"}
]
}
]
}