Aggregate invocation · group by
---
config:
flowchart:
useMaxWidth: false
---
flowchart TB
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["Aggregate invocation · group by<br/>🟢4"]:::ok
subgraph G_ds["Dataset"]
direction LR
v_group_by["agg(DS group by Id)"]:::ok
v_group_by_two["agg(DS group by Id, Id)"]:::ok
v_no_group["agg(DS)"]:::ok
end
subgraph G_comp["Component"]
direction LR
v_aggr_multiple["DS#91;aggr Me #58;#61; agg(Me), Me #58;#61; agg(Me) group by Id#93;"]:::ok
end
subgraph G_use["Used with"]
direction LR
used__clause__aggr["aggr ↗<br/><small>used in</small>"]:::ext
used__time__time_agg["time_agg ↗<br/><small>uses, in group all</small>"]:::ext
end
O --- G_ds
O --- G_comp
O -.- G_use
style G_ds fill:#f8f9fa,stroke:#495057,stroke-width:2px
style G_comp fill:#f8f9fa,stroke:#495057,stroke-width:2px,stroke-dasharray: 8 5
style G_use fill:#ffffff,stroke:#868e96,stroke-width:1px,stroke-dasharray: 1 4
click v_group_by href "#form-group-by"
click v_group_by_two href "#form-group-by-two"
click v_no_group href "#form-no-group"
click v_aggr_multiple href "#form-aggr-multiple"
click used__clause__aggr href "../clause/aggr.html" "Aggregation · aggr"
click used__time__time_agg href "../time/time_agg.html" "Time aggregation · time_agg"
Types
Takes any scalar · Gives any type
Comes from: any operator that returns a value
Goes to: whatever takes its input's type
Forms
agg(DS group by Id) 🟢 · any scalar → any type
Manual example 1 🟢 pandas ✅ · DuckDB ✅
- Tests:
avgat data set level grouped byId_1, on an operand with the non-viral attributeAt_1.- Manual:
Aggregate invocation, Example 1, output verbatim: “For invocation at Data Set level, the Attribute propagation rule is applied”.
- Expected:
At_1is dropped, as a non-viral attribute; 2010 gives (20 + 1 + 1 + 23) / 4 = 11.25, 2011 gives 10 and 2012 gives 35.- Erratum:
the script comments its two alternative forms with
#, which is the membership operator, not a VTL comment. The manual’s conventions define/* */and//, so they are commented with//here.
DS_r := avg ( DS_1 group by Id_1 );
// Or equivalently:
// DS_r := avg ( DS_1 group except Id_2, Id_3 )
// DS_r := avg ( DS_1#Me_1 group by Id_1 )
| Id_1 | Id_2 | Id_3 | Me_1 | At_1 |
|---|---|---|---|---|
| 2010 | E | XX | 20 | |
| 2010 | B | XX | 1 | H |
| 2010 | R | XX | 1 | A |
| 2010 | F | YY | 23 | |
| 2011 | E | XX | 20 | P |
| 2011 | B | ZZ | 1 | N |
| 2011 | R | YY | -1 | P |
| 2011 | F | XX | 20 | Z |
| 2012 | L | ZZ | 40 | P |
| 2012 | E | YY | 30 | P |
| Id_1 | Me_1 |
|---|---|
| 2010 | 11.25 |
| 2011 | 10.0 |
| 2012 | 35.0 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
agg(DS group by Id, Id) 🟢 · any scalar → any type
Manual example 2 🟢 pandas ✅ · DuckDB ✅
- Tests:
sumgrouped by two identifiers,Id_1andId_3.- Manual:
Aggregate invocation, Example 2, with the manual’s data points: “the Data Points are grouped by the values of the specified Identifiers. The Identifiers not specified are dropped in the result.”
- Expected:
one data point per pair, without
Id_2: 2010,XXgives 20 + 1 + 1 = 22.Me_1may be Integer, as the manual prints it, or Number: the Sum page 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 := sum ( DS_1 group by Id_1, Id_3 );
| Id_1 | Id_2 | Id_3 | Me_1 | At_1 |
|---|---|---|---|---|
| 2010 | E | XX | 20 | |
| 2010 | B | XX | 1 | H |
| 2010 | R | XX | 1 | A |
| 2010 | F | YY | 23 | |
| 2011 | E | XX | 20 | P |
| 2011 | B | ZZ | 1 | N |
| 2011 | R | YY | -1 | P |
| 2011 | F | XX | 20 | Z |
| 2012 | L | ZZ | 40 | P |
| 2012 | E | YY | 30 | P |
| Id_1 | Id_3 | Me_1 |
|---|---|---|
| 2010 | XX | 22 |
| 2010 | YY | 23 |
| 2011 | XX | 40 |
| 2011 | ZZ | 1 |
| 2011 | YY | -1 |
| 2012 | ZZ | 40 |
| 2012 | YY | 30 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
agg(DS) 🟢 · any scalar → any type
Manual example 3 🟢 pandas ✅ · DuckDB ✅
- Tests:
avgwith no grouping clause: the whole operand is one group.- Manual:
Aggregate invocation, Example 3, output verbatim: “If no grouping clause is specified, then all the input Data Points are aggregated in a single group and the operator returns a Data Set that contains a single Data Point and has no Identifiers.”
- Expected:
one data point with no identifiers:
Me_1= 155 / 10 = 15.5, a Number, and the non-viralAt_1is dropped.
DS_r := avg ( DS_1 );
| Id_1 | Id_2 | Id_3 | Me_1 | At_1 |
|---|---|---|---|---|
| 2010 | E | XX | 20 | |
| 2010 | B | XX | 1 | H |
| 2010 | R | XX | 1 | A |
| 2010 | F | YY | 23 | |
| 2011 | E | XX | 20 | P |
| 2011 | B | ZZ | 1 | N |
| 2011 | R | YY | -1 | P |
| 2011 | F | XX | 20 | Z |
| 2012 | L | ZZ | 40 | P |
| 2012 | E | YY | 30 | P |
| Me_1 |
|---|
| 15.5 |
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), Me := agg(Me) group by Id] 🟢 · any scalar → any type
Manual example 4 🟢 pandas ✅ · DuckDB ✅
- Tests:
two aggregates of one measure in
aggr,maxandmingrouped byId_1, on an operand with the non-viral attributeAt_1.- Manual:
Aggregate invocation, Example 4: “the Attributes calculated within the aggr clause are maintained in the result; for all the other Attributes that are defined as viral, the Attribute propagation rule is applied”.
- Expected:
Id_1,Me_2andMe_3, Integer asMe_1: 23 and 1 for 2010, 20 and -1 for 2011, 40 and 30 for 2012.- Erratum:
the manual’s result is named
DS_3and keepsAt_1as a ViralAttribute, but the script assignsDS_randDS_1declaresAt_1a plain Attribute. The User Manual says the Attribute “is considered as non-viral” when its virality is not defined, soAt_1, neither calculated nor viral, is dropped, as in the page’s other three examples.
DS_r := DS_1 [ aggr Me_2 := max ( Me_1 ) , Me_3 := min ( Me_1 ) group by Id_1 ];
| Id_1 | Id_2 | Id_3 | Me_1 | At_1 |
|---|---|---|---|---|
| 2010 | E | XX | 20 | |
| 2010 | B | XX | 1 | H |
| 2010 | R | XX | 1 | A |
| 2010 | F | YY | 23 | |
| 2011 | E | XX | 20 | P |
| 2011 | B | ZZ | 1 | N |
| 2011 | R | YY | -1 | P |
| 2011 | F | XX | 20 | Z |
| 2012 | L | ZZ | 40 | P |
| 2012 | E | YY | 30 | P |
| Id_1 | Me_2 | Me_3 |
|---|---|---|
| 2010 | 23 | 1 |
| 2011 | 20 | -1 |
| 2012 | 40 | 30 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗