@@ -249,6 +249,49 @@ The third query aggregates the personality types.
249249Finally, the fourth query is another conditional query which subsumes multiple base personality types, if present, to a new personality.
250250
251251
252+ === Using `NEXT` inside a conditional query using `{}`
253+
254+ In a conditional query that that has a `NEXT` in any of its `THEN` blocks or the `ELSE` block, wrap the part after `THEN` or `ELSE` with `{}`.
255+
256+ .`NEXT` inside a conditional query
257+ [source, cypher]
258+ ----
259+ MATCH (c:Customer)-[:BUYS]->(p:Product)
260+ RETURN c AS customer, sum(p.price) AS sum
261+
262+ NEXT
263+
264+ WHEN sum >= 1000 THEN {
265+ RETURN customer.firstName AS customer, "club 1000 plus" AS customerType, sum AS sum
266+ }
267+ ELSE {
268+ RETURN customer AS customer, sum * (1 - customer.discount) AS finalSum
269+
270+ NEXT
271+
272+ RETURN customer.firstName AS customer, "club below 1000" AS customerType, finalSum AS sum
273+ }
274+ ----
275+
276+ .Result
277+ [role="queryresult",options="header,footer",cols="3*<m"]
278+ |===
279+ | customer | customerType | sum
280+
281+ | "Amir" | "club 1000 plus" | 1005
282+ | "Mateo" | "club 1000 plus" | 1015
283+ | "Leila" | "club 1000 plus" | 1000
284+ | "Yusuf" | "club 1000 plus" | 1005
285+ | "Keisha" | "club below 1000" | 200.0
286+ | "Hannah" | "club below 1000" | 221.0
287+ | "Niko" | "club below 1000" | 570.0
288+
289+ 3+d|Rows: 3
290+ |===
291+
292+ The query above calculates the total price of products purchased per customer and then only applies the customer discount to sums below 1000.
293+
294+
252295== Interactions with `UNION` queries
253296
254297.`NEXT` in a query using `UNION`
@@ -284,4 +327,40 @@ RETURN customer AS customer, count(customer) as numberOfProducts
284327In this example, the list of customer names from the first query has a duplicate entry for "Mateo" who bought both a laptop and coffee.
285328The use of `UNION ALL` added him to the list twice.
286329The second query can access the list, because both parts of the `UNION` return a part of the list, aliased as `customer`.
287- By using xref:functions/aggregating.adoc#functions-count[`count()`], the list aggregates the duplicate in the `RETURN` part of the query.
330+ By using xref:functions/aggregating.adoc#functions-count[`count()`], the list aggregates the duplicate in the `RETURN` part of the query.
331+
332+
333+ === Using `NEXT` inside a `UNION` using `{}`
334+
335+ In a `UNION` query that that has a `NEXT` in any of its blocks, wrap that block with `{}`.
336+
337+ .`NEXT` inside `UNION`
338+ [source, cypher]
339+ ----
340+ {
341+ RETURN 1 AS a
342+
343+ NEXT
344+
345+ RETURN a + 1 AS b
346+ }
347+ UNION ALL
348+ {
349+ RETURN 1 AS a
350+
351+ NEXT
352+
353+ RETURN a + 1 AS b
354+ }
355+ ----
356+
357+ .Result
358+ [role="queryresult",options="header,footer",cols="1*<m"]
359+ |===
360+ | b
361+
362+ | 2
363+ | 2
364+
365+ 1+d|Rows: 2
366+ |===
0 commit comments