Skip to content

UNWIND

The UNWIND statement splits a list into separated rows.

UNWIND can function as an individual statement or a clause in a statement.

Syntax

UNWIND <list> AS <alias> <RETURN clause>;

Split a list

The following example splits the list [1,2,3] into three rows.

nebula> UNWIND [1,2,3] AS n RETURN n;
+---+
| n |
+---+
| 1 |
| 2 |
| 3 |
+---+

Return a list with distinct items

Use WITH DISTINCT in the UNWIND statement to return a list with distinct items.

Example 1

The following statement:

  1. Splits the list [1,1,2,2,3,3] into rows.
  2. Removes duplicated rows.
  3. Sorts the rows.
  4. Transforms the rows to a list.
nebula> WITH [1,1,2,2,3,3] AS n \
        UNWIND n AS r \
        WITH DISTINCT r AS r \
        ORDER BY r \
        RETURN collect(r);
+------------+
| collect(r) |
+------------+
| [1, 2, 3]  |
+------------+

Example 2

The following statement:

  1. Outputs the vertices on the matched path into a list.
  2. Splits the list into rows.
  3. Removes duplicated rows.
  4. Transforms the rows to a list.
nebula> MATCH p=(v:player{name:"Tim Duncan"})--(v2) \
        WITH nodes(p) AS n \
        UNWIND n AS r \
        WITH DISTINCT r AS r \
        RETURN collect(r);
+----------------------------------------------------------------------------------------------------------------------+
| collect(r)                                                                                                           |
+----------------------------------------------------------------------------------------------------------------------+
| [("player100" :player{age: 42, name: "Tim Duncan"}), ("player101" :player{age: 36, name: "Tony Parker"}),            | 
|("team204" :team{name: "Spurs"}), ("player102" :player{age: 33, name: "LaMarcus Aldridge"}),                          |
|("player125" :player{age: 41, name: "Manu Ginobili"}), ("player104" :player{age: 32, name: "Marco Belinelli"}),       |
|("player144" :player{age: 47, name: "Shaquile O'Neal"}), ("player105" :player{age: 31, name: "Danny Green"}),         |
|("player113" :player{age: 29, name: "Dejounte Murray"}), ("player107" :player{age: 32, name: "Aron Baynes"}),         |
|("player109" :player{age: 34, name: "Tiago Splitter"}), ("player108" :player{age: 36, name: "Boris Diaw"})]           |
+----------------------------------------------------------------------------------------------------------------------+

Last update: September 16, 2022