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
Copy file name to clipboardExpand all lines: modules/ROOT/pages/clauses/with.adoc
+19-22Lines changed: 19 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -135,10 +135,6 @@ RETURN chocolateCustomers,
135
135
Variable `p` not defined
136
136
----
137
137
138
-
[NOTE]
139
-
The `WHERE` subclause is not limited in the same way by preceding `WITH` clauses.
140
-
For more information, see xref:clauses/where.adoc#where-and-with[Using `WHERE` after `WITH`].
141
-
142
138
.Retain all variables with `WITH *`
143
139
[source, cypher]
144
140
----
@@ -256,9 +252,7 @@ ORDER BY price
256
252
|===
257
253
258
254
[NOTE]
259
-
The `LET` clause can also be used to assign values to variables.
260
-
However, unlike `WITH`, it cannot drop variables from the query scope.
261
-
As a result, `LET` can be used to chain expressions in a more clear and concise manner than `WITH`.
255
+
The `LET` clause can be used to assign values to variables and to chain expressions more clearly and concisely than `WITH`.
262
256
For more information, see xref:clauses/let.adoc#chaining-expressions[`LET` -> Chaining expressions].
263
257
264
258
[[aggregations]]
@@ -362,17 +356,18 @@ ORDER BY discountRates
362
356
[[ordering-pagination]]
363
357
== Ordering and pagination
364
358
365
-
`WITH` can order and paginate results before they are passed on to subsequent clauses.
359
+
`WITH` can order and paginate results if used together with the xref:clauses/order-by.adoc[`ORDER BY`], xref:clauses/limit.adoc[`LIMIT`], and xref:clauses/skip.adoc[`SKIP`] subclauses.
360
+
If so, these subclauses be understood as part of the result manipulation performed by `WITH` -- not as a standalone clause -- before results are passed on to subsequent clauses.
366
361
367
-
In the below query, the results are ordered in a descending order by which `Customer` has spent the most using xref:clauses/order-by.adoc[`ORDER BY`] before they are passed on to the final `RETURN` clause.
362
+
In the below query, the results are ordered in a descending order by which `Customer` has spent the most using `ORDER BY` before they are passed on to the final `RETURN` clause.
368
363
369
364
.Order results with `ORDER BY`
370
365
[source, cypher]
371
366
----
372
367
MATCH (c:Customer)-[:BUYS]->(p:Product)
373
368
WITH c,
374
369
sum(p.price) AS totalSpent
375
-
ORDER BY totalSpent DESC
370
+
ORDER BY totalSpent DESC
376
371
RETURN c.firstName AS customer, totalSpent
377
372
----
378
373
@@ -392,7 +387,7 @@ RETURN c.firstName AS customer, totalSpent
392
387
2+d|Rows: 7
393
388
|===
394
389
395
-
In the next example, xref:clauses/limit.adoc[`LIMIT`] is used to only retain the top 3 customers with the highest `totalSpent` values in the result set after ordering.
390
+
In the next example, `LIMIT` is used to only retain the top 3 customers with the highest `totalSpent` values in the result set after ordering.
396
391
Then, the xref:clauses/set.adoc[`SET`] assigns a new property (`topSpender = true`) to those customers who have spent the most.
397
392
398
393
.Limit results with `LIMIT`
@@ -401,8 +396,8 @@ Then, the xref:clauses/set.adoc[`SET`] assigns a new property (`topSpender = tru
401
396
MATCH (c:Customer)-[:BUYS]->(p:Product)
402
397
WITH c,
403
398
sum(p.price) AS totalSpent
404
-
ORDER BY totalSpent DESC
405
-
LIMIT 3
399
+
ORDER BY totalSpent DESC
400
+
LIMIT 3
406
401
SET c.topSpender = true
407
402
RETURN c.firstName AS customer,
408
403
totalSpent,
@@ -420,7 +415,7 @@ RETURN c.firstName AS customer,
420
415
3+d|Rows: 3
421
416
|===
422
417
423
-
xref:clauses/skip.adoc[`SKIP`] can be used after a `WITH` clause to discard rows from the result set.
418
+
`SKIP` can be used after a `WITH` clause to discard rows from the result set.
424
419
Below, `SKIP` excludes the first 3 rows in the ordered result set (i.e. the 3 `Customer` nodes with highest `totalSpent` value) and assigns a `false` value to the new `topSpender` property of the remaining `Customer` nodes.
425
420
426
421
.Exclude results with `SKIP`
@@ -429,8 +424,8 @@ Below, `SKIP` excludes the first 3 rows in the ordered result set (i.e. the 3 `C
429
424
MATCH (c:Customer)-[:BUYS]->(p:Product)
430
425
WITH c,
431
426
sum(p.price) AS totalSpent
432
-
ORDER BY totalSpent DESC
433
-
SKIP 3
427
+
ORDER BY totalSpent DESC
428
+
SKIP 3
434
429
SET c.topSpender = false
435
430
RETURN c.firstName AS customer,
436
431
totalSpent,
@@ -459,8 +454,8 @@ The second `MATCH` clause then matches only from that single product to find all
459
454
----
460
455
MATCH (:Supplier {name: 'Foodies Inc.'})-[:SUPPLIES]->(p:Product)
461
456
WITH p
462
-
ORDER BY p.price DESC
463
-
LIMIT 1
457
+
ORDER BY p.price DESC
458
+
LIMIT 1
464
459
MATCH (p)<-[:BUYS]-(c:Customer)
465
460
RETURN p.name AS product
466
461
p.price AS price,
@@ -481,14 +476,16 @@ RETURN p.name AS product
481
476
[[filter-results]]
482
477
== Filter results
483
478
484
-
`WITH` can be used in conjunction with `WHERE` to filter results.
479
+
`WITH` can be followed by the xref:clauses/where.adoc[`WHERE`] subclause to filter results.
480
+
Similar to the subclauses used for xref:clauses/with.adoc#ordering-pagination[ordering and pagination], `WHERE` should be understood as part of the result manipulation performed by `WITH` -- not as a standalone clause -- before the results are passed on to subsequent clauses.
481
+
For more information, see xref:clauses/where.adoc#where-and-with[Using `WHERE` after `WITH`].
485
482
486
483
.Filter using `WITH` and `WHERE`
487
484
[source, cypher]
488
485
----
489
486
UNWIND [1, 2, 3, 4, 5, 6] AS x
490
487
WITH x
491
-
WHERE x > 2
488
+
WHERE x > 2
492
489
RETURN x
493
490
----
494
491
@@ -504,7 +501,7 @@ RETURN x
504
501
1+d|Rows: 4
505
502
|===
506
503
507
-
In the below query, the `WITH` and `WHERE` are used to filter out any `Supplier` nodes whose `totalSales` is less than `1000`.
504
+
In the below query, `WITH` and `WHERE` are used to filter out any `Supplier` nodes whose `totalSales` is less than `1000`.
508
505
Note the use of `DISTINCT` inside `collect()` to remove any duplicate `Customer` nodes.
509
506
510
507
.Filter property values using `WITH` and `WHERE`
@@ -514,7 +511,7 @@ MATCH (s:Supplier)-[:SUPPLIES]->(p:Product)<-[:BUYS]-(c:Customer)
0 commit comments