← aggr operator page · clause/aggr/having/manual_ex_3

Manual example 3 🟢 all ok

pandas ✅ · DuckDB ✅

Tests:

aggr Me_1 := sum( Me_1 ), Me_2 := max( Me_1) group by Id_1 , Id_2 with having avg (Me_1 ) > 2.

Manual:

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).

Expected:

(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".

script.vtl ⇩

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 ];

input/DS_1.csv ⇩

Id_1Id_2Id_3Me_1
1AXX0
1AYY2
1BXX3
1BYY5
2AXX7
2AYY2

input/DS_1.json ⇩

{
  "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}
      ]
    }
  ]
}

expected/DS_r.csv ⇩

Id_1Id_2Me_1Me_2
1B85
2A97

expected/DS_r.json ⇩

{
  "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"}
      ]
    }
  ]
}