Average value · avg

🟢 8 of 8 forms all ok · VTL 2.2 Reference Manual ↗ · vtlengine 1.10.0rc3 (9e1c81a6) · pandas 2.3.3 · DuckDB 1.5.5
🟢 all ok🟡 partially implemented🛡️ refused with an error🔴 not implemented or crash🔵 engines differ ✅ match · ❌ wrong result · 🛡 guarded error (code) · 💥 crash · 📌 team decision
Dataset formsComponent formsScalar forms
        ---
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["Average value · avg<br/>🟢8"]:::ok
    subgraph G_ds["Dataset"]
        direction LR
        v_ds_no_group["avg(DS)"]:::ok
        v_ds_group_by["avg(DS group by Id)"]:::ok
        v_ds_group_except["avg(DS group except Id)"]:::ok
        v_ds_group_all["avg(DS group all time_agg(#quot;A#quot;))"]:::ok
        v_ds_having["avg(DS group by Id having cond)"]:::ok
        v_analytic_ds["avg(DS over (partition by Id))"]:::ok
    end
    subgraph G_comp["Component"]
        direction LR
        v_comp_in_aggr["DS#91;aggr Me #58;#61; avg(Me) group by Id#93;"]:::ok
        v_analytic_comp["DS#91;calc Me #58;#61; avg(Me over (partition by Id))#93;"]:::ok
    end
    subgraph G_use["Used with"]
        direction LR
        used__clause__aggr["aggr ↗<br/><small>used in</small>"]:::ext
        used__clause__calc["calc ↗<br/><small>used in over()</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_ds_no_group href "#form-ds-no-group"
    click v_ds_group_by href "#form-ds-group-by"
    click v_ds_group_except href "#form-ds-group-except"
    click v_ds_group_all href "#form-ds-group-all"
    click v_ds_having href "#form-ds-having"
    click v_comp_in_aggr href "#form-comp-in-aggr"
    click v_analytic_ds href "#form-analytic-ds"
    click v_analytic_comp href "#form-analytic-comp"
    click used__clause__aggr href "../clause/aggr.html" "Aggregation · aggr"
    click used__clause__calc href "../clause/calc.html" "Calculation of a Component · calc"
    

Types

Takes number, boolean · Gives number

Viral attributes: combined with the propagation rules → Viral propagation

Comes from · number, boolean — 60 operators in 8 blocks 🟢60
General purpose operators (2) 🟢2
String operators (3) 🟢3
Numeric operators (18) 🟢18
Comparison operators (12) 🟢12
Boolean operators (4) 🟢4
Time operators (9) 🟢9
Aggregate and Analytic operators (9) 🟢9
Data validation operators (3) 🟢3
Comes from · operators that return any type (their input's) — 37 operators in 8 blocks 🟢37
General purpose operators (5) 🟢5
Join operators (4) 🟢4
Time operators (3) 🟢3
Set operators (4) 🟢4
Hierarchical aggregation (1) 🟢1
Aggregate and Analytic operators (8) 🟢8
Conditional operators (3) 🟢3
Clause operators (9) 🟢9
Goes to · number — 33 operators in 7 blocks 🟢33
General purpose operators (1) 🟢1
Numeric operators (18) 🟢18
Time operators (2) 🟢2
Hierarchical aggregation (1) 🟢1
Aggregate and Analytic operators (7) 🟢7
Data validation operators (2) 🟢2
Definitions (VTL-DL) (2) 🟢2
Goes to · operators that take any scalar — 48 operators in 8 blocks 🟢48
General purpose operators (6) 🟢6
Join operators (4) 🟢4
Comparison operators (11) 🟢11
Set operators (4) 🟢4
Aggregate and Analytic operators (10) 🟢10
Data validation operators (1) 🟢1
Conditional operators (3) 🟢3
Clause operators (9) 🟢9

Forms

avg(DS) 🟢 · number → number

Single group no identifiers 🟢 pandas ✅ · DuckDB ✅
Tests:

avg with no grouping clause, over the shared input with two Number measures and no null.

Manual:

Aggregate invocation: “If the grouping clause is omitted, 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: Me_1 = 22 / 6 = 3.666667 and Me_2 = 19 / 6 = 3.166667, the averages, sum / n.

script.vtl
DS_r := avg ( DS_1 );
Input DS_1
Id_1Id_2Me_1Me_2
A2020Q131.500000
A2020Q213.500000
A2021Q152.500000
A2021Q210.500000
B2020Q146.500000
B2020Q284.500000
Expected DS_r
Me_1Me_2
3.6666673.166667

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

avg(DS group by Id) 🟢 · number → number

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

avg at data set level grouped by Id_1, over one Integer measure.

Manual:

Average value, Example 1, output verbatim: “The operator returns the average of the input values.”

Expected:

5.0 for 2011 and 3.0 for 2012: “The result is always a number, including when all operand values are integer.”

script.vtl
DS_r := avg ( DS_1 group by Id_1 );
Input DS_1
Id_1Id_2Id_3Me_1
2011AXX3
2011AYY5
2011BYY7
2012AXX2
2012BYY4
Expected DS_r
Id_1Me_1
20115.0
20123.0

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

Null left out of average 🟢 pandas ✅ · DuckDB ✅
Tests:

avg grouped by Id_1 over a measure with one null, the Me_1 of A, 2021Q1.

Manual:

the User Manual: “the input Data Points having a null value are in general not considered. In the average, for example, they are not considered both in the numerator (the sum) and in the denominator (the count).”

Expected:

(3 + 1 + 1) / 3 = 1.666667 for A, where counting the null would give 1.25, and 6 for B, as Numbers: “The result is always a number”.

script.vtl
DS_r := avg ( DS_1 group by Id_1 );
Input DS_1
Id_1Id_2Me_1
A2020Q13
A2020Q21
A2021Q1
A2021Q21
B2020Q14
B2020Q28
Expected DS_r
Id_1Me_1
A1.666667
B6

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

avg(DS group except Id) 🟢 · number → number

Drops the listed identifier 🟢 pandas ✅ · DuckDB ✅
Tests:

avg with group except Id_2: the data points are grouped by Id_1, the identifier that it does not name.

Manual:

Aggregate invocation: “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 and the two measures, without Id_2: 2.5 and 2 for A, 6 and 5.5 for B, the averages, sum / n.

script.vtl
DS_r := avg ( DS_1 group except Id_2 );
Input DS_1
Id_1Id_2Me_1Me_2
A2020Q131.500000
A2020Q213.500000
A2021Q152.500000
A2021Q210.500000
B2020Q146.500000
B2020Q284.500000
Expected DS_r
Id_1Me_1Me_2
A2.5000002
B65.500000

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

avg(DS group all time_agg("A")) 🟢 · number → number

Quarters to years 🟢 pandas ✅ · DuckDB ✅
Tests:

avg with the deprecated group all time_agg ( "A" ), which turns each quarter of Id_2 into its year, written 2020 or 2021.

Manual:

Aggregate invocation: “group all: converts the values of an Identifier Component using conversionExpr and keeps all the resulting Identifiers”; Time aggregation: “On time_period type, the operator maps the input value into the comprising larger time period specified by the periodIndTo parameter”.

Expected:

Id_1, the year and the two measures: 2 and 2.5 for A, 2020, 3 and 1.5 for A, 2021 and 6 and 5.5 for B, 2020.

script.vtl
DS_r := avg ( DS_1 group all time_agg ( "A" ) );
Input DS_1
Id_1Id_2Me_1Me_2
A2020Q131.500000
A2020Q213.500000
A2021Q152.500000
A2021Q210.500000
B2020Q146.500000
B2020Q284.500000
Expected DS_r
Id_1Id_2Me_1Me_2
A202022.500000
A202131.500000
B202065.500000

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

avg(DS group by Id having cond) 🟢 · number, boolean → number

Two measures 🟢 pandas ✅ · DuckDB ✅
Tests:

avg with having, over the two measures of the shared input.

Manual:

Aggregate invocation: the havingCondition “must invoke aggregate operators”, and “only groups for which havingCondition evaluates to true appear in the result”. The condition aggregates Me_1 only, where the manual and the reading of #1119 agree.

Expected:

avg ( Me_1 ) > 3 is 2.5 for A and 6 for B, so only B is kept, with 6 and 5.5.

script.vtl
DS_r := avg ( DS_1 group by Id_1 having avg ( Me_1 ) > 3 );
Input DS_1
Id_1Id_2Me_1Me_2
A2020Q131.500000
A2020Q213.500000
A2021Q152.500000
A2021Q210.500000
B2020Q146.500000
B2020Q284.500000
Expected DS_r
Id_1Me_1Me_2
B65.500000

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

One measure 🟢 pandas ✅ · DuckDB ✅
Tests:

avg with the having of Two measures, on a single-measure operand: the shared input without Me_2.

Manual:

Aggregate invocation: “only groups for which havingCondition evaluates to true appear in the result”; the page does not restrict having to single-measure operands.

Expected:

avg ( Me_1 ) > 3 holds for B only, which is kept with Me_1 = 6.

script.vtl
DS_r := avg ( DS_1 group by Id_1 having avg ( Me_1 ) > 3 );
Input DS_1
Id_1Id_2Me_1
A2020Q13
A2020Q21
A2021Q15
A2021Q21
B2020Q14
B2020Q28
Expected DS_r
Id_1Me_1
B6

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

avg(DS over (partition by Id)) 🟢 · number → number

Whole partition without order 🟢 pandas ✅ · DuckDB ✅
Tests:

avg as an analytic function at data set level, partitioned by Id_1, with no order and no window clause.

Manual:

Analytic invocation: “If windowClause is not specified, then the set of Data Points which contribute to the analytic operation is the whole partition which the current Data Point belongs to”. With no order clause, the two defaults that the page gives for an omitted window agree.

Expected:

every data point gets the values of its partition: 2.5 and 2 in A, 6 and 5.5 in B.

script.vtl
DS_r := avg ( DS_1 over ( partition by Id_1 ) );
Input DS_1
Id_1Id_2Me_1Me_2
A2020Q131.500000
A2020Q213.500000
A2021Q152.500000
A2021Q210.500000
B2020Q146.500000
B2020Q284.500000
Expected DS_r
Id_1Id_2Me_1Me_2
A2020Q12.5000002
A2020Q22.5000002
A2021Q12.5000002
A2021Q22.5000002
B2020Q165.500000
B2020Q265.500000

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

DS[aggr Me := avg(Me) group by Id] 🟢 · number → number

Keeps only calculated measure 🟢 pandas ✅ · DuckDB ✅
Tests:

avg at component level in aggr, grouped by Id_1.

Manual:

Aggregate invocation: “For the invocation at Component level, the resulting Data Set has the Measures explicitly calculated (all the other Measures are dropped because no aggregation behaviour is specified for them).”

Expected:

Id_1 and Me_3 only: 2.5 for A and 6 for B, the averages, sum / n.

script.vtl
DS_r := DS_1 [ aggr Me_3 := avg ( Me_1 ) group by Id_1 ];
Input DS_1
Id_1Id_2Me_1Me_2
A2020Q131.500000
A2020Q213.500000
A2021Q152.500000
A2021Q210.500000
B2020Q146.500000
B2020Q284.500000
Expected DS_r
Id_1Me_3
A2.500000
B6

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

DS[calc Me := avg(Me over (partition by Id))] 🟢 · number → number

Whole partition without order 🟢 pandas ✅ · DuckDB ✅
Tests:

avg as an analytic function in calc, partitioned by Id_1, with no order and no window clause.

Manual:

Analytic invocation: “the resulting Data Set retains all the original components of the input Data Set plus the components explicitly calculated by it”; with no order and no window clause, the window is the whole partition.

Expected:

the input, plus Me_3 = 2.5 on the data points of A and 6 on those of B.

script.vtl
DS_r := DS_1 [ calc Me_3 := avg ( Me_1 over ( partition by Id_1 ) ) ];
Input DS_1
Id_1Id_2Me_1Me_2
A2020Q131.500000
A2020Q213.500000
A2021Q152.500000
A2021Q210.500000
B2020Q146.500000
B2020Q284.500000
Expected DS_r
Id_1Id_2Me_1Me_2Me_3
A2020Q131.5000002.500000
A2020Q213.5000002.500000
A2021Q152.5000002.500000
A2021Q210.5000002.500000
B2020Q146.5000006
B2020Q284.5000006

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗