Aggregation · aggr
---
config:
flowchart:
useMaxWidth: false
---
flowchart LR
classDef ok fill:#d4edda,stroke:#28a745,color:#000
classDef partial fill:#fff3cd,stroke:#ffc107,color:#000
classDef refused fill:#d4edda,stroke:#28a745,stroke-width:3px,stroke-dasharray: 2 2,color:#000
classDef missing fill:#f8d7da,stroke:#dc3545,color:#000
classDef diverge fill:#cce5ff,stroke:#007bff,color:#000
classDef block fill:#2b3a4a,stroke:#2b3a4a,color:#fff,font-weight:bold
classDef ext fill:#fff,stroke:#666,color:#000,stroke-dasharray: 5 5
O["Aggregation · aggr 📌<br/>🟢9"]:::ok
v_no_group["DS#91;aggr Me #58;#61; agg(Me)#93;"]:::ok
v_group_by["DS#91;aggr Me #58;#61; agg(Me) group by Id#93;"]:::ok
v_group_except["DS#91;aggr Me #58;#61; agg(Me) group except Id#93;"]:::ok
v_group_all["DS#91;aggr Me #58;#61; agg(Me) group all time_agg(#quot;A#quot;)#93;"]:::ok
v_having["DS#91;aggr Me #58;#61; agg(Me) group by Id having cond#93;"]:::ok
v_measure_role["DS#91;aggr measure Me #58;#61; agg(Me) group by Id#93;"]:::ok
v_attribute_role["DS#91;aggr attribute At #58;#61; agg(Me) group by Id#93;"]:::ok
v_viral_attribute_role["DS#91;aggr viral attribute At #58;#61; agg(Me) group by Id#93; 📌"]:::ok
v_role_omitted_attribute["DS#91;aggr At #58;#61; agg(At) group by Id#93;"]:::ok
subgraph G_use["Used with"]
direction LR
used__join["Join operators (4)"]:::ext
used__time["Time operators (1)"]:::ext
used__aggregate["Aggregate and Analytic operators (11)"]:::ext
end
v_no_group --- O
v_group_by --- O
v_group_except --- O
v_group_all --- O
v_having --- O
v_measure_role --- O
v_attribute_role --- O
O --- v_viral_attribute_role
O --- v_role_omitted_attribute
O -.- G_use
style G_use fill:#ffffff,stroke:#868e96,stroke-width:1px,stroke-dasharray: 1 4
click v_no_group href "#form-no-group"
click v_group_by href "#form-group-by"
click v_group_except href "#form-group-except"
click v_group_all href "#form-group-all"
click v_having href "#form-having"
click v_measure_role href "#form-measure-role"
click v_attribute_role href "#form-attribute-role"
click v_viral_attribute_role href "#form-viral-attribute-role"
click v_role_omitted_attribute href "#form-role-omitted-attribute"
click used__join href "../join/index.html"
click used__time href "../time/index.html"
click used__aggregate href "../aggregate/index.html"
Used with — 16 operators in 3 blocks 🟢16
Join operators (4) 🟢4
Time operators (1) 🟢1
Aggregate and Analytic operators (11) 🟢11
group by usesCounting the number of data points count usesMinimum value min usesMaximum value max usesMedian value median usesSum sum usesAverage value avg usesPopulation standard deviation stddev_pop usesSample standard deviation stddev_samp usesPopulation variance var_pop usesSample variance var_samp usesTypes
Takes boolean, any scalar · Gives any type
Comes from: any operator that returns a value
Goes to: whatever takes its input's type
Forms
DS[aggr Me := agg(Me)] 🟢 · any scalar → any type
Single group no identifiers 🟢 pandas ✅ · DuckDB ✅
- Tests:
aggr Me_1 := sum ( Me_1 )with no grouping clause.- Manual:
Aggregation: “If no grouping clause is specified, then all the input Data Points are aggregated in a single group and the clause returns a Data Set that contains a single Data Point and has no Identifiers.”
- Expected:
One data point with no identifiers: the Measure
Me_1(no role written, andMe_1is a Measure ofDS_1) = 2 + 5 + 3 = 10. It may be Integer or Number: Sum says “implementations may preserve the integer type for the resulting measure or component when all operand values are integer and the sum is computed in integer arithmetic”.
DS_r := DS_1 [ aggr Me_1 := sum ( Me_1 ) ];
| Id_1 | Id_2 | Me_1 |
|---|---|---|
| 1 | A | 2 |
| 1 | B | 5 |
| 2 | A | 3 |
| Me_1 |
|---|
| 10 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
DS[aggr Me := agg(Me) group by Id] 🟢 · any scalar → any type
Manual example 1 🟢 pandas ✅ · DuckDB ✅
- Tests:
aggr Me_1 := sum( Me_1 ) group by Id_1 , Id_2, over an Integer measure.- Manual:
Aggregation, Example 1, with the manual’s data points: “group by: the Data Points are grouped by the values of the specified Identifiers (groupingId). The Identifiers not specified are dropped in the result.”
- Expected:
Id_3is dropped, and the sums are 2, 8 and 9.Me_1may be Integer, as the manual’sex_1.jsonwrites it, or Number: Sum says “implementations may preserve the integer type for the resulting measure or component when all operand values are integer and the sum is computed in integer arithmetic”.
DS_r := DS_1 [ aggr Me_1:= sum( Me_1 ) group by Id_1 , Id_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 |
| Id_1 | Id_2 | Me_1 |
|---|---|---|
| 1 | A | 2 |
| 1 | B | 8 |
| 2 | A | 9 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
DS[aggr Me := agg(Me) group except Id] 🟢 · any scalar → any type
Manual example 2 🟢 pandas ✅ · DuckDB ✅
- Tests:
aggr Me_3 := min( Me_1 ) group except Id_3, over an Integer measure.- Manual:
Aggregation, Example 2, output verbatim: “group except: the Data Points are grouped by the values of the Identifiers not specified as groupingId. The Identifiers specified as groupingId are dropped in the result.”
- Expected:
Id_1,Id_2and the smallestMe_1of each group, 0, 3 and 2.Me_3stays an Integer, as the manual’sex_2.jsonwrites it: unlikesum,minallows no other type, as Minimun value says “For numeric operands, the result has the same type as the operand”.
DS_r := DS_1 [ aggr Me_3:= min( Me_1 ) group except Id_3 ];
| 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 |
| Id_1 | Id_2 | Me_3 |
|---|---|---|
| 1 | A | 0 |
| 1 | B | 3 |
| 2 | A | 2 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
DS[aggr Me := agg(Me) group all time_agg("A")] 🟢 · any scalar → any type
Quarters to years 🟢 pandas ✅ · DuckDB ✅
- Tests:
aggr Me_1 := sum ( Me_1 ) group all time_agg ( "A" ), on the quarterly time_period identifierId_1.- Manual:
Aggregation: group all “converts the values of an Identifier Component using conversionExpr and keeps all the resulting Identifiers”. Time aggregation: in an aggregation “op must not be specified, and the source dataset must have exactly one Identifier of type time, date or time_period”, and on time_period “the original period indicator is converted in the target one”.
- Expected:
The data points are grouped by the annual
Id_1and byId_2:(2020A, A)= 1 + 2 = 3,(2021A, A)= 4 and(2020A, B)= 8, whereMe_1may be Integer or Number, by the integer rule of the Sum page. Aggregate invocation marks group all with “This option is deprecated”; the Aggregation page does not.
DS_r := DS_1 [ aggr Me_1 := sum ( Me_1 ) group all time_agg ( "A" ) ];
| Id_1 | Id_2 | Me_1 |
|---|---|---|
| 2020Q1 | A | 1 |
| 2020Q2 | A | 2 |
| 2021Q1 | A | 4 |
| 2020Q1 | B | 8 |
| Id_1 | Id_2 | Me_1 |
|---|---|---|
| 2020A | A | 3 |
| 2021A | A | 4 |
| 2020A | B | 8 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
DS[aggr Me := agg(Me) group by Id having cond] 🟢 · any scalar, boolean → any type
Manual example 3 🟢 pandas ✅ · DuckDB ✅
- Tests:
aggr Me_1 := sum( Me_1 ), Me_2 := max( Me_1) group by Id_1 , Id_2withhaving 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” (
avghere, 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_1may be Integer, as the manual writes it, or Number, by the integer rule of Sum, andMe_2stays an Integer, as formax“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 |
| Id_1 | Id_2 | Me_1 | Me_2 |
|---|---|---|---|
| 1 | B | 8 | 5 |
| 2 | A | 9 | 7 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
DS[aggr measure Me := agg(Me) group by Id] 🟢 · any scalar → any type
Only the new measure kept 🟢 pandas ✅ · DuckDB ✅
- Tests:
aggr measure Me_2 := sum ( Me_1 ) group by Id_1: a new component, with the rolemeasurewritten.- Manual:
Aggregation:
aggrRole ::= measure | attribute | viral attribute, and “The aggr computed Measures and Attributes are the only Measures and Attributes returned in the output Data Set (plus the possible viral Attributes)”.- Expected:
Id_1and the MeasureMe_2, the sums 1 + 5 = 6 and 3 + 4 = 7:group by Id_1dropsId_2, andMe_1is not in the result.Me_2may be Integer or Number: Sum says “implementations may preserve the integer type for the resulting measure or component when all operand values are integer and the sum is computed in integer arithmetic”.
DS_r := DS_1 [ aggr measure Me_2 := sum ( Me_1 ) group by Id_1 ];
| Id_1 | Id_2 | Me_1 |
|---|---|---|
| 1 | A | 1 |
| 1 | B | 5 |
| 2 | A | 3 |
| 2 | B | 4 |
| Id_1 | Me_2 |
|---|---|
| 1 | 6 |
| 2 | 7 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
DS[aggr attribute At := agg(Me) group by Id] 🟢 · any scalar → any type
Attribute next to measure 🟢 pandas ✅ · DuckDB ✅
- Tests:
aggr Me_1 := sum ( Me_1 ), attribute At_1 := max ( Me_1 ) group by Id_1: a new attribute next to the aggregated measure.- Manual:
Aggregation: “It is possible to specify the role of the calculated Component among measure, attribute, or viral attribute”, and “The Attributes calculated through the aggr clauses are maintained in the result”.
- Expected:
Per
Id_1, withId_2dropped, the sumsMe_1= 6 and 7 and the maximaAt_1= 5 and 4, an Integer Attribute, as formax“For numeric operands, the result has the same type as the operand”;Me_1may be Integer or Number, by the integer rule of Sum. TheMe_1item keeps a Measure in the result: Attribute without measure leaves it out.
DS_r := DS_1 [ aggr Me_1 := sum ( Me_1 ), attribute At_1 := max ( Me_1 ) group by Id_1 ];
| Id_1 | Id_2 | Me_1 |
|---|---|---|
| 1 | A | 1 |
| 1 | B | 5 |
| 2 | A | 3 |
| 2 | B | 4 |
| Id_1 | Me_1 | At_1 |
|---|---|---|
| 1 | 6 | 5 |
| 2 | 7 | 4 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
Attribute without measure 🟢 pandas ✅ · DuckDB ✅
- Tests:
aggr attribute At_1 := max ( Me_1 ) group by Id_1: an attribute as the only aggregated component.- Manual:
Aggregation: “The aggr computed Measures and Attributes are the only Measures and Attributes returned in the output Data Set”; the User Manual: “There can be from 0 to N Measures in a Data Structure. A Data Set without Measures is allowed”.
- Expected:
Id_1and the AttributeAt_1= 5 and 4, the maxima, with no Measure:Me_1is not in the result.At_1is an Integer, as formax“For numeric operands, the result has the same type as the operand”.
DS_r := DS_1 [ aggr attribute At_1 := max ( Me_1 ) group by Id_1 ];
| Id_1 | Id_2 | Me_1 |
|---|---|---|
| 1 | A | 1 |
| 1 | B | 5 |
| 2 | A | 3 |
| 2 | B | 4 |
| Id_1 | At_1 |
|---|---|
| 1 | 5 |
| 2 | 4 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
DS[aggr At := agg(At) group by Id] 🟢 · any scalar → any type
Existing attribute keeps role 🟢 pandas ✅ · DuckDB ✅
- Tests:
aggr Me_1 := sum ( Me_1 ), At_1 := max ( At_1 ) group by Id_1with no role:At_1is an Attribute ofDS_1.- Manual:
Aggregation: “When the role is omitted, the following rule is applied: if the component exists in the operand Data Set then it maintains its role”, and “The Attributes calculated through the aggr clauses are maintained in the result”.
- Expected:
Per
Id_1, the MeasureMe_1= 6 and 7, Integer or Number by the integer rule of the Sum page, and the Integer AttributeAt_1= 7 and 9, as formax“For numeric operands, the result has the same type as the operand”.At_1stays non-viral, as “a non-viral Attribute in the input generates a non-viral Attribute also in the result” (User Manual).
DS_r := DS_1 [ aggr Me_1 := sum ( Me_1 ), At_1 := max ( At_1 ) group by Id_1 ];
| Id_1 | Id_2 | Me_1 | At_1 |
|---|---|---|---|
| 1 | A | 1 | 7 |
| 1 | B | 5 | 2 |
| 2 | A | 3 | 4 |
| 2 | B | 4 | 9 |
| Id_1 | Me_1 | At_1 |
|---|---|---|
| 1 | 6 | 7 |
| 2 | 7 | 9 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗