Rank · rank

🟢 2 of 2 forms all ok · VTL 2.2 Reference Manual ↗ · vtlengine 1.10.0rc2 (90ee9376) · 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
        ---
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["Rank · rank<br/>🟢2"]:::ok
    v_partition_order["DS#91;calc Me #58;#61; rank(over (partition by Id order by Me))#93;"]:::ok
    v_order_only["DS#91;calc Me #58;#61; rank(over (order by Id))#93;"]:::ok
    subgraph G_use["Used with"]
        direction LR
        used__clause__calc["calc ↗<br/><small>used in over()</small>"]:::ext
    end
    v_partition_order --- O
    v_order_only --- O
    O -.- G_use
    style G_use fill:#ffffff,stroke:#868e96,stroke-width:1px,stroke-dasharray: 1 4
    click v_partition_order href "#form-partition-order"
    click v_order_only href "#form-order-only"
    click used__clause__calc href "../clause/calc.html" "Calculation of a Component · calc"
    

Types

Takes any scalar · Gives integer

Viral attributes: combined with the propagation rules → Viral propagation

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
Clause operators (9) 🟢9

Forms

DS[calc Me := rank(over (partition by Id order by Me))] 🟢 · any scalar → integer

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

rank in calc, partitioned by Id_1 and Id_2 and ordered by Me_1, with a tie.

Manual:

Rank, Example 1, output verbatim: “If some Data Points are in the same order according to the specified orderClause, the same order number (rank) is assigned and a gap appears in the sequence of the assigned ranks”.

Expected:

in XX, 3, 4, 6 and 7 rank 1 to 4; in YY, the two 5s rank 1, then 9 ranks 3 and 10 ranks 4.

script.vtl
DS_r := DS_1 [ calc Me_2 := rank ( over ( partition by Id_1 , Id_2 order by Me_1 ) ) ];
Input DS_1
Id_1Id_2Id_3Me_1Me_2
AXX200031
AXX200149
AXX200275
AXX200368
AYY200093
AYY200154
AYY2002102
AYY200357
Expected DS_r
Id_1Id_2Id_3Me_1Me_2
AXX200031
AXX200142
AXX200274
AXX200363
AYY200093
AYY200151
AYY2002104
AYY200351

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

DS[calc Me := rank(over (order by Id))] 🟢 · any scalar → integer

Default partition by other identifiers 🟢 pandas ✅ · DuckDB ✅
Tests:

rank with an order clause and no partition clause.

Manual:

Analytic invocation: “If the clause is omitted, then the Data Set is partitioned by the Identifier Components that are not specified in the orderClause”; the order names Id_2 only, so the partition is by Id_1.

Expected:

1 to 4 for the quarters of A, 1 and 2 for those of B; ranking the whole data set, as SQL does without PARTITION BY, would give 1, 3, 5, 6, 1, 3.

script.vtl
DS_r := DS_1 [ calc Me_3 := rank ( over ( order by 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_1Id_2Me_1Me_2Me_3
A2020Q131.5000001
A2020Q213.5000002
A2021Q152.5000003
A2021Q210.5000004
B2020Q146.5000001
B2020Q284.5000002

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