5.5.3 Using the ELEMENT_NUMBER Function
The ELEMENT_NUMBER function can only be used for iterator
variables that are defined in the ONE ROW PER VERTEX or ONE ROW PER
STEP clause in a SQL graph query. It returns the sequential element number of
the vertex or edge that the referenced iterator variable currently binds to.
Since paths always start with a vertex and alternate between vertices and
edges, the first element is a vertex with element number 1, the second element is an
edge with element number 2, the third element is a vertex with element number 3, and so
on. For example, consider the following path: v1 -[e1]-> v2 -[e2]->
v3. Then the element numbers are generated from left to right as shown:
v1:1, e1:2, v2:3, e2:4, v3:5
Therefore, vertices always have odd element numbers while edges have even element numbers.
If ONE ROW PER STEP is specified and the path is empty (that is, only
has a single vertex and no edges), then ELEMENT_NUMBER returns
NULL when the iterator edge variable or the second iterator vertex
variable is referenced. Note that empty paths result in single steps in which only the
first iterator (vertex) variable is bound.
Also, note the following:
ELEMENT_NUMBERcan be used only in theCOLUMNSclause.ELEMENT_NUMBERfunction can only be used ifONE ROW PER VERTEXorONE ROW PER STEPis specified in the SQL graph query.ELEMENT_NUMBERcannot reference any type of variable other than an iterator variable.
For instance, consider the following sample query. The query uses ONE ROW PER
STEP and the ELEMENT_NUMBER function returns the element
number of the graph element that is bound by the iterator variable
v.
SELECT *
FROM GRAPH_TABLE ( g
MATCH (a) ->{0,2} ONE ROW PER STEP (v,e,v2)
COLUMNS (ELEMENT_NUMBER(v) AS elementNumber)
)See Example 5-23 for more information.
Parent topic: Using ONE ROW PER Clause in a SQL Graph Query