Skip to content

Commit 6bd23a1

Browse files
cheat sheet tags
1 parent 9d46a1a commit 6bd23a1

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

modules/ROOT/pages/clauses/with.adoc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,15 @@ In the below example, the `WITH` clause binds all matched `Customer` nodes to a
8989
The bound nodes are `MAP` values which can then be referenced from the new variable.
9090

9191
.Create a new variable bound to matched nodes
92+
// tag::clauses_with_new_variable[]
9293
[source, cypher]
9394
----
9495
MATCH (c:Customer)-[:BUYS]->(:Product {name: 'Chocolate'})
9596
WITH c AS customer
9697
RETURN customer.firstName AS chocolateCustomer
9798
----
99+
// end::clauses_with_new_variable[]
100+
98101

99102
.Result
100103
[role="queryresult",options="header,footer",cols="1*<m"]
@@ -137,6 +140,7 @@ Variable `p` not defined
137140
----
138141

139142
.Retain all variables with `WITH *`
143+
// tag::clauses_with_all_variables[]
140144
[source, cypher]
141145
----
142146
MATCH (supplier:Supplier)-[r]->(product:Product)
@@ -145,6 +149,7 @@ RETURN supplier.name AS company,
145149
type(r) AS relType,
146150
product.name AS product
147151
----
152+
// end::clauses_with_all_variables[]
148153

149154
.Result
150155
[role="queryresult",options="header,footer",cols="3*<m"]
@@ -166,6 +171,7 @@ More specifically, a variable imported into a `CALL` subquery will be available
166171
In the below example, the `x` variable is imported to the inside scope of a `CALL` subquery, and is successfully referenced by the `RETURN` clause even though the preceding `WITH` neglects to list it.
167172

168173
.Variables cannot be de-scoped in the inner scope of a subquery
174+
// tag::clauses_with_subquery[]
169175
[source, cypher]
170176
----
171177
WITH 11 AS x
@@ -176,6 +182,8 @@ CALL (x) {
176182
}
177183
RETURN x, a
178184
----
185+
// end::clauses_with_subquery[]
186+
179187

180188
.Result
181189
[role="queryresult",options="header,footer",cols="2*<m"]
@@ -197,6 +205,7 @@ For more information, see xref:subqueries/call-subquery.adoc#import-variables[`C
197205
In the below query, the value of the `STRING` concatenation expression is bound to a new variable `customerFullName`, and the value from the expression `chocolate.price * (1 - customer.discount)` is bound to `chocolateNetPrice`, both of which are then available in the `RETURN` clause.
198206

199207
.Bind values to variables
208+
// tag::clauses_with_bind_values[]
200209
[source, cypher]
201210
----
202211
MATCH (customer:Customer)-[:BUYS]->(chocolate:Product {name: 'Chocolate'})
@@ -205,6 +214,8 @@ WITH customer.firstName || ' ' || customer.lastName AS customerFullName,
205214
RETURN customerFullName,
206215
chocolateNetPrice
207216
----
217+
// end::clauses_with_bind_values[]
218+
208219

209220
.Result
210221
[role="queryresult",options="header,footer",cols="2*<m"]
@@ -221,6 +232,7 @@ RETURN customerFullName,
221232
Because `WITH` can be used to assign variables to the values of expressions, it can be used to chain expressions.
222233

223234
.Chain expressions using `WITH`
235+
// tag::clauses_with_chain_expressions[]
224236
[source, cypher]
225237
----
226238
MATCH (p:Product)
@@ -237,6 +249,7 @@ RETURN p.name AS product,
237249
discountCategory
238250
ORDER BY price
239251
----
252+
// end::clauses_with_chain_expressions[]
240253

241254
.Result
242255
[role="queryresult",options="header,footer", cols="4*<m"]
@@ -261,6 +274,7 @@ In this example, the xref:functions/aggregating.adoc#functions-sum[`sum()`] func
261274
The xref:functions/aggregating.adoc#functions-collect[`collect()`] function is used to collect each product into `LIST` values bound to the `productsBought` variable.
262275

263276
.`WITH` performing aggregations
277+
// tag::clauses_with_aggregations[]
264278
[source, cypher]
265279
----
266280
MATCH (c:Customer)-[:BUYS]->(p:Product)
@@ -272,6 +286,8 @@ RETURN customer,
272286
productsBought
273287
ORDER BY totalSpent DESC
274288
----
289+
// end::clauses_with_aggregations[]
290+
275291

276292
.Result
277293
[role="queryresult",options="header,footer", cols="3*<m"]
@@ -297,13 +313,15 @@ ORDER BY totalSpent DESC
297313
In the below query, `WITH DISTINCT` is used to remove any duplicate `discount` property values from `Customer` nodes.
298314

299315
.`WITH DISTINCT` to remove duplicate values
316+
// tag::clauses_with_remove_duplicates[]
300317
[source, cypher]
301318
----
302319
MATCH (c:Customer)
303320
WITH DISTINCT c.discount AS discountRates
304321
RETURN discountRates
305322
ORDER BY discountRates
306323
----
324+
// end::clauses_with_remove_duplicates[]
307325

308326
.Result
309327
[role="queryresult",options="header,footer", cols="1*<m"]
@@ -357,6 +375,7 @@ In the next example, `LIMIT` is used to only retain the top 3 customers with the
357375
Then, the xref:clauses/set.adoc[`SET`] assigns a new property (`topSpender = true`) to those customers who have spent the most.
358376

359377
.Limit results with `LIMIT`
378+
// tag::clauses_with_ordering_pagination[]
360379
[source, cypher]
361380
----
362381
MATCH (c:Customer)-[:BUYS]->(p:Product)
@@ -369,6 +388,8 @@ RETURN c.firstName AS customer,
369388
totalSpent,
370389
c.topSpender AS topSpender
371390
----
391+
// end::clauses_with_ordering_pagination[]
392+
372393

373394
[role="queryresult",options="header,footer", cols="3*<m"]
374395
|===
@@ -470,6 +491,7 @@ In the below query, `WITH` and `WHERE` are used to filter out any `Supplier` nod
470491
Note the use of `DISTINCT` inside `collect()` to remove any duplicate `Customer` nodes.
471492

472493
.Filter property values using `WITH` and `WHERE`
494+
// tag::clauses_with_filtering[]
473495
[source, cypher]
474496
----
475497
MATCH (s:Supplier)-[:SUPPLIES]->(p:Product)<-[:BUYS]-(c:Customer)
@@ -481,6 +503,7 @@ RETURN s.name AS supplier,
481503
totalSales,
482504
uniqueCustomers
483505
----
506+
// end::clauses_with_filtering[]
484507

485508
[role="queryresult",options="header,footer", cols="3*<m"]
486509
|===
@@ -498,6 +521,7 @@ RETURN s.name AS supplier,
498521
If a write clause is followed by a read clause, `WITH` must be used as a separator between the two.
499522

500523
.`WITH` used as a separator between a write clause and a read clause
524+
// tag::clauses_with_combine_write_read[]
501525
[source, cypher]
502526
----
503527
MATCH (yusuf:Customer {firstName: 'Yusuf'}),
@@ -511,6 +535,7 @@ RETURN collect(p.name) AS yusufPurchases,
511535
r.date AS date
512536
ORDER BY date DESC
513537
----
538+
// end::clauses_with_combine_write_read[]
514539

515540
[role="queryresult",options="header,footer", cols="2*<m"]
516541
|===

0 commit comments

Comments
 (0)