You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* `NEXT` can improve the modularity and readability of complex queries.
11
11
* `NEXT` can be used instead of xref:clauses/with.adoc[] clause to construct complex queries.
12
-
* `NEXT` can improve the usability of xref:queries/composed-queries/conditional-queries.adoc[conditional `WHEN`].
13
-
* `NEXT` allows for passing the full table of intermediate results into the arms of xref:queries/composed-queries/combined-queries.adoc[combined `UNION`] queries.
12
+
* `NEXT` can improve the usability of xref:queries/composed-queries/conditional-queries.adoc[].
13
+
* `NEXT` allows for passing the full table of intermediate results into the arms of xref:queries/composed-queries/combined-queries.adoc[].
14
14
15
15
[[example-graph]]
16
16
== Example graph
@@ -82,9 +82,7 @@ NEXT
82
82
[[passing-values]]
83
83
== Passing values to subsequent queries
84
84
85
-
`NEXT` passes the full whole table of intermediate results to the subsequent query.
86
-
This is referred to by-table semantics, which is different from `CALL` subqueries which passes values using by-row semantics e.g., one row at the time.
87
-
By-table semantics are particularly useful when aggregating values.
85
+
`NEXT` passes the result table of a query to the subsequent query.
88
86
In the following example, `NEXT` passes the variable `customer` to the second query:
89
87
90
88
.Passing a variable to another query via `NEXT`
@@ -112,7 +110,6 @@ RETURN customer.firstName AS chocolateCustomer
112
110
|===
113
111
114
112
.Passing multiple variables to another query via `NEXT`
115
-
// tag::sequential_queries_basic_example[]
116
113
[source,cypher]
117
114
----
118
115
MATCH (c:Customer)-[:BUYS]->(p:Product {name: 'Chocolate'})
@@ -123,7 +120,6 @@ NEXT
123
120
RETURN customer.firstName AS chocolateCustomer,
124
121
product.price * (1 - customer.discount) AS chocolatePrice
@@ -144,66 +140,66 @@ Literals or unaliased expressions are not allowed.
144
140
For example, `RETURN 1` and `RETURN 1 + 1` cannot precede `NEXT`, but `RETURN 1 AS one` and `RETURN 1 + 1 AS two` can.
145
141
====
146
142
147
-
Variables which are local to a query and which are not explicitly returned are not accessible by subsequent queries in the context of `NEXT`.
143
+
Variables which are local to the query preceding `NEXT` and which are not explicitly returned as part of the result of that query are not accessible by subsequent queries in the context of `NEXT`.
148
144
This allows you to control variable scope similarly to what you can do with `WITH`, see xref:clauses/with.adoc#variable-scope[Control variables in scope].
149
145
150
-
[[next-and-union]]
151
-
== Interactions with `UNION` queries
146
+
[[aggregation-after-next]]
147
+
== Aggregation after `NEXT`
148
+
149
+
`NEXT` passes the result table as a whole to the subsequent query.
150
+
This particularly useful when aggregating values.
151
+
152
+
In the following example, `NEXT` passes the variable `customer` to the second query:
152
153
153
-
.`NEXT` in a query using `UNION`
154
+
.Aggregation after `NEXT`
154
155
[source,cypher]
155
156
----
156
-
MATCH (c:Customer)-[:BUYS]->(:Product{name: "Laptop"})
157
-
RETURN c.firstName AS customer
158
-
UNION ALL
159
-
MATCH (c:Customer)-[:BUYS]-> (:Product{name: "Coffee"})
160
-
RETURN c.firstName AS customer
157
+
MATCH (c:Customer)-[:BUYS]->(p:Product)
158
+
RETURN c AS customer, p AS product
161
159
162
160
NEXT
163
161
164
-
RETURN customer AS customer, count(customer) as numberOfProducts
In this example, we compute for each non-coffee product the percentage of customers that also bought coffee.
356
+
So for each product `p`, the subquery find all pairs of a customer `c` of product `p` and another product `otherProduct` that customer has also bought.
357
+
The first `NEXT` passes these pairs as a whole into a `UNION`, so that the query can
358
+
(1) count all customers in the first arm of the union and
359
+
(2) count the customers who also bought coffee in the second arm of the union.
360
+
The `UNION` produce two rows -- one from each arm.
361
+
The second `NEXT` passes these two rows as a whole into a query the aggregates them into a single row, which is the result of the `CALL` subquery.
362
+
317
363
[NOTE]
318
364
====
319
365
`NEXT` cannot be used inside a `CALL` subquery that uses the (deprecated) xref:subqueries/call-subquery.adoc#importing-with[importing `WITH`] syntax.
320
366
====
321
367
322
368
[[next-and-conditional-queries]]
323
369
== Interactions with conditional queries
324
-
Conditional queries act similar to `CALL` by processing incoming rows using by-row semantics.
370
+
371
+
[[conditional-queries-inside-next]]
372
+
=== Using conditional query before or after `NEXT`
373
+
374
+
Conditional queries act similar to `CALL` by processing the incoming table of intermediate result row-by-row.
325
375
A conditional query following a `NEXT` acts equivalent to a conditional query wrapped in a `CALL` subquery.
0 commit comments