Counting the number of data points · count
---
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["Counting the number of data points · count 📌<br/>🟢9"]:::ok
subgraph G_ds["Dataset"]
direction LR
v_ds_no_group["count(DS) 📌"]:::ok
v_ds_group_by["count(DS group by Id) 📌"]:::ok
v_ds_group_except["count(DS group except Id) 📌"]:::ok
v_ds_group_all["count(DS group all time_agg(#quot;A#quot;)) 📌"]:::ok
v_ds_having["count(DS group by Id having cond) 📌"]:::ok
v_count_in_having["count(DS group by Id having count() #62; n)"]:::ok
v_analytic_ds["count(DS over (partition by Id))"]:::ok
end
subgraph G_comp["Component"]
direction LR
v_comp_in_aggr["DS#91;aggr Me #58;#61; count(Me) group by Id#93;"]:::ok
v_analytic_comp["DS#91;calc Me #58;#61; count(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_count_in_having href "#form-count-in-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 boolean, any scalar · Gives integer
Comes from: any operator that returns a value
Goes to · integer — 44 operators in 8 blocks 🟢44
General purpose operators (1) 🟢1
String operators (2) 🟢2
Numeric operators (18) 🟢18
Time operators (6) 🟢6
Hierarchical aggregation (1) 🟢1
Aggregate and Analytic operators (10) 🟢10
Data validation operators (2) 🟢2
Definitions (VTL-DL) (4) 🟢4
Goes to · operators that take any scalar — 45 operators in 8 blocks 🟢45
General purpose operators (6) 🟢6
Join operators (4) 🟢4
Comparison operators (11) 🟢11
Set operators (4) 🟢4
Aggregate and Analytic operators (7) 🟢7
Data validation operators (1) 🟢1
Conditional operators (3) 🟢3
Forms
count(DS) 🟢 · any scalar → integer
Single group no identifiers 🟢 pandas ✅ · DuckDB ✅
- Tests:
countwith 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, with
int_var= 6, the number of data points.- Team decision:
#1049: over a data set,
countgives one Integer measureint_varwhatever the measures of the operand, and ignores the data points whose measures are all null. With two measures, this settles the conflict between the count page’sint_varand the Aggregate invocation’s “the resulting Data Set has the same Measures as the operand”.
DS_r := count ( DS_1 );
| Id_1 | Id_2 | Me_1 | Me_2 |
|---|---|---|---|
| A | 2020Q1 | 3 | 1.500000 |
| A | 2020Q2 | 1 | 3.500000 |
| A | 2021Q1 | 5 | 2.500000 |
| A | 2021Q2 | 1 | 0.500000 |
| B | 2020Q1 | 4 | 6.500000 |
| B | 2020Q2 | 8 | 4.500000 |
| int_var |
|---|
| 6 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
count(DS group by Id) 🟢 · any scalar → integer
Manual example 1 🟢 pandas ✅ · DuckDB ✅
- Tests:
countat data set level grouped byId_1, over one String measure.- Manual:
Counting the number of data points, Example 1, output verbatim: “The operator returns the number of the input Data Points.” The result type is
dataset { measure<integer> int_var }.- Expected:
int_var= 3 for 2011 and 2 for 2012. With one measure and no null, #1049 gives the same output.
DS_r := count ( DS_1 group by Id_1 );
| Id_1 | Id_2 | Id_3 | Me_1 |
|---|---|---|---|
| 2011 | A | XX | iii |
| 2011 | A | YY | jjj |
| 2011 | B | YY | iii |
| 2012 | A | XX | kkk |
| 2012 | B | YY | iii |
| Id_1 | int_var |
|---|---|
| 2011 | 3 |
| 2012 | 2 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
All null data point ignored 🟢 pandas ✅ · DuckDB ✅
- Tests:
which data points
countcounts when measures are null: inA, 2020Q2 has both measures null and 2021Q1 onlyMe_2.- Manual:
Counting the number of data points: “The operator returns the number of the input Data Points”, which counts 4 in
A; the User Manual: “the input Data Points having a null value are in general not considered”, which counts 2.- Expected:
int_var= 3 forA, without 2020Q2, and 2 forB.- Team decision:
#1049: “A Data Point whose Measures are all null is ignored; a Data Point holding at least one non-null Measure is counted”, and the data set form “always returns a single Measure
int_varof type Integer”.
DS_r := count ( DS_1 group by Id_1 );
| Id_1 | Id_2 | Me_1 | Me_2 |
|---|---|---|---|
| A | 2020Q1 | 3 | 1.5 |
| A | 2020Q2 | ||
| A | 2021Q1 | 5 | |
| A | 2021Q2 | 1 | 0.5 |
| B | 2020Q1 | 4 | 6.5 |
| B | 2020Q2 | 8 | 4.5 |
| Id_1 | int_var |
|---|---|
| A | 3 |
| B | 2 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
count(DS group except Id) 🟢 · any scalar → integer
Drops the listed identifier 🟢 pandas ✅ · DuckDB ✅
- Tests:
countwithgroup except Id_2: the data points are grouped byId_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_1andint_var, withoutId_2: 4 forAand 2 forB, the number of data points of each group.- Team decision:
#1049: over a data set,
countgives one Integer measureint_varwhatever the measures of the operand, and ignores the data points whose measures are all null. With two measures, this settles the conflict between the count page’sint_varand the Aggregate invocation’s “the resulting Data Set has the same Measures as the operand”.
DS_r := count ( DS_1 group except Id_2 );
| Id_1 | Id_2 | Me_1 | Me_2 |
|---|---|---|---|
| A | 2020Q1 | 3 | 1.500000 |
| A | 2020Q2 | 1 | 3.500000 |
| A | 2021Q1 | 5 | 2.500000 |
| A | 2021Q2 | 1 | 0.500000 |
| B | 2020Q1 | 4 | 6.500000 |
| B | 2020Q2 | 8 | 4.500000 |
| Id_1 | int_var |
|---|---|
| A | 4 |
| B | 2 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
count(DS group all time_agg("A")) 🟢 · any scalar → integer
Quarters to years 🟢 pandas ✅ · DuckDB ✅
- Tests:
countwith the deprecatedgroup all time_agg ( "A" ), which turns each quarter ofId_2into its year.- 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 andint_var= 2 forA, 2020,A, 2021andB, 2020.- Team decision:
#1049: over a data set,
countgives one Integer measureint_varwhatever the operand’s measures, and ignores the data points whose measures are all null. With two measures, this settles the conflict between the count page’sint_varand the Aggregate invocation’s “the resulting Data Set has the same Measures as the operand”.
DS_r := count ( DS_1 group all time_agg ( "A" ) );
| Id_1 | Id_2 | Me_1 | Me_2 |
|---|---|---|---|
| A | 2020Q1 | 3 | 1.500000 |
| A | 2020Q2 | 1 | 3.500000 |
| A | 2021Q1 | 5 | 2.500000 |
| A | 2021Q2 | 1 | 0.500000 |
| B | 2020Q1 | 4 | 6.500000 |
| B | 2020Q2 | 8 | 4.500000 |
| Id_1 | Id_2 | int_var |
|---|---|---|
| A | 2020 | 2 |
| A | 2021 | 2 |
| B | 2020 | 2 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
count(DS group by Id having cond) 🟢 · any scalar, boolean → integer
Two measures 🟢 pandas ✅ · DuckDB ✅
- Tests:
countwithhaving, 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_1only, where the manual and the reading of #1119 agree.- Expected:
count ( Me_1 ) > 2is 4 forAand 2 forB, so onlyAis kept, withint_var= 4.- Team decision:
#1049: over a data set,
countgives one Integer measureint_varwhatever the measures of the operand, and ignores the data points whose measures are all null. With two measures, this settles the conflict between the count page’sint_varand the Aggregate invocation’s “the resulting Data Set has the same Measures as the operand”.
DS_r := count ( DS_1 group by Id_1 having count ( Me_1 ) > 2 );
| Id_1 | Id_2 | Me_1 | Me_2 |
|---|---|---|---|
| A | 2020Q1 | 3 | 1.500000 |
| A | 2020Q2 | 1 | 3.500000 |
| A | 2021Q1 | 5 | 2.500000 |
| A | 2021Q2 | 1 | 0.500000 |
| B | 2020Q1 | 4 | 6.500000 |
| B | 2020Q2 | 8 | 4.500000 |
| Id_1 | int_var |
|---|---|
| A | 4 |
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:
countwith thehavingof Two measures, on a single-measure operand: the shared input withoutMe_2.- Manual:
Aggregate invocation: “only groups for which havingCondition evaluates to true appear in the result”; the page does not restrict
havingto single-measure operands.- Expected:
count ( Me_1 ) > 2holds forAonly, which is kept withint_var= 4. With one measure and no null, #1049 gives the manual’s ownint_var.
DS_r := count ( DS_1 group by Id_1 having count ( Me_1 ) > 2 );
| Id_1 | Id_2 | Me_1 |
|---|---|---|
| A | 2020Q1 | 3 |
| A | 2020Q2 | 1 |
| A | 2021Q1 | 5 |
| A | 2021Q2 | 1 |
| B | 2020Q1 | 4 |
| B | 2020Q2 | 8 |
| Id_1 | int_var |
|---|---|
| A | 4 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
count(DS group by Id having count() > n) 🟢 · any scalar, boolean → integer
Manual example 2 🟢 pandas ✅ · DuckDB ✅
- Tests:
count ( )without operand in ahavingcondition.- Manual:
Counting the number of data points, Example 2, output verbatim; the Aggregate invocation: “The count operator is used in a havingCondition without parameters”.
- Expected:
2011 has 3 data points and is kept, 2012 has 2 and is dropped:
int_var= 3 for 2011. With one measure and no null, #1049 gives the same output.
DS_r := count ( DS_1 group by Id_1 having count() > 2 );
| Id_1 | Id_2 | Id_3 | Me_1 |
|---|---|---|---|
| 2011 | A | XX | iii |
| 2011 | A | YY | jjj |
| 2011 | B | YY | iii |
| 2012 | A | XX | kkk |
| 2012 | B | YY | iii |
| Id_1 | int_var |
|---|---|
| 2011 | 3 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
count(DS over (partition by Id)) 🟢 · any scalar → integer
Whole partition without order 🟢 pandas ✅ · DuckDB ✅
- Tests:
countas an analytic function at data set level, partitioned byId_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”.
- Expected:
int_var= 4 on the data points ofAand 2 on those ofB: the count page’s result typedataset { measure<integer> int_var }is read as prevailing over the invocation’s “same Identifiers and Measures as the input Data Set”. #1049 covers the Aggregate invocation only (PR #1050), so it does not apply here.
DS_r := count ( DS_1 over ( partition by Id_1 ) );
| Id_1 | Id_2 | Me_1 | Me_2 |
|---|---|---|---|
| A | 2020Q1 | 3 | 1.500000 |
| A | 2020Q2 | 1 | 3.500000 |
| A | 2021Q1 | 5 | 2.500000 |
| A | 2021Q2 | 1 | 0.500000 |
| B | 2020Q1 | 4 | 6.500000 |
| B | 2020Q2 | 8 | 4.500000 |
| Id_1 | Id_2 | int_var |
|---|---|---|
| A | 2020Q1 | 4 |
| A | 2020Q2 | 4 |
| A | 2021Q1 | 4 |
| A | 2021Q2 | 4 |
| B | 2020Q1 | 2 |
| B | 2020Q2 | 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 := count(Me) group by Id] 🟢 · any scalar → integer
Keeps only calculated measure 🟢 pandas ✅ · DuckDB ✅
- Tests:
count ( Me_1 )at component level inaggr, grouped byId_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_1and the IntegerMe_3only: 4 forAand 2 forB, the non-null values ofMe_1, as #1049 keeps the component forms as they are.
DS_r := DS_1 [ aggr Me_3 := count ( Me_1 ) group by Id_1 ];
| Id_1 | Id_2 | Me_1 | Me_2 |
|---|---|---|---|
| A | 2020Q1 | 3 | 1.500000 |
| A | 2020Q2 | 1 | 3.500000 |
| A | 2021Q1 | 5 | 2.500000 |
| A | 2021Q2 | 1 | 0.500000 |
| B | 2020Q1 | 4 | 6.500000 |
| B | 2020Q2 | 8 | 4.500000 |
| Id_1 | Me_3 |
|---|---|
| A | 4 |
| B | 2 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
DS[calc Me := count(Me over (partition by Id))] 🟢 · any scalar → integer
Whole partition without order 🟢 pandas ✅ · DuckDB ✅
- Tests:
count ( Me_1 )as an analytic function incalc, partitioned byId_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 the Integer
Me_3= 4 on the data points ofAand 2 on those ofB.
DS_r := DS_1 [ calc Me_3 := count ( Me_1 over ( partition by Id_1 ) ) ];
| Id_1 | Id_2 | Me_1 | Me_2 |
|---|---|---|---|
| A | 2020Q1 | 3 | 1.500000 |
| A | 2020Q2 | 1 | 3.500000 |
| A | 2021Q1 | 5 | 2.500000 |
| A | 2021Q2 | 1 | 0.500000 |
| B | 2020Q1 | 4 | 6.500000 |
| B | 2020Q2 | 8 | 4.500000 |
| Id_1 | Id_2 | Me_1 | Me_2 | Me_3 |
|---|---|---|---|---|
| A | 2020Q1 | 3 | 1.500000 | 4 |
| A | 2020Q2 | 1 | 3.500000 | 4 |
| A | 2021Q1 | 5 | 2.500000 | 4 |
| A | 2021Q2 | 1 | 0.500000 | 4 |
| B | 2020Q1 | 4 | 6.500000 | 2 |
| B | 2020Q2 | 8 | 4.500000 | 2 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗