Skip to content

Commit e7adf62

Browse files
add count example
1 parent b5ca1b2 commit e7adf62

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed

modules/ROOT/pages/queries/composed-queries/conditional-queries.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ If no `WHEN` branches are executed and an `ELSE` branch exists, it is executed.
5252
If no `WHEN` branches evaluates to `true` and no `ELSE` branch is present, no branches are executed and no rows are produced.
5353
The following examples demonstrates this logic:
5454

55-
.`WHEN` logic
55+
.Conditional logic
5656
[source, cypher]
5757
----
5858
WHEN false THEN RETURN 1 AS x
@@ -247,7 +247,7 @@ RETURN n.name AS name, ageGroup, manager
247247
| "Charlie" | "Veteran" | [["Daniel", "Senior"]]
248248
| "Daniel" | "Senior" | [["Peter", "Senior"]]
249249
| "Eskil" | "Senior" | [["Peter", "Senior"]]
250-
| "Peter" | "Senior" | [[NULL, NULL]]
250+
| "Peter" | "Senior" | \[[NULL, NULL]]
251251
252252
3+d| Rows: 6
253253
|===

modules/ROOT/pages/subqueries/call-subquery.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ Now all `Player` nodes, regardless of whether they have any `PLAYS_FOR` relation
488488
[[conditional-call]]
489489
== Conditional `CALL` subqueries
490490

491-
`WHEN` can be used inside `CALL` subqueries to execute branches only when a condition evaluates to `true`.
491+
`WHEN` can be used inside `CALL` subqueries to execute branches conditionally when a predicate evaluates to `true`.
492492
Note that the names and the number of columns returned by the different `WHEN` branches must be identical.
493493
For more information, see xref:queries/composed-queries/conditional-queries.adoc#conditional-subqueries[Conditional queries -> Conditional subqueries].
494494

modules/ROOT/pages/subqueries/collect.adoc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@ RETURN person.name as name, COLLECT {
7474
2+d|Rows: 3
7575
|===
7676

77+
[role=label--new-2025.02]
78+
[[conditional-collect]]
7779
== Conditional `COLLECT` subquery
7880

79-
`WHEN` can be used inside `COLLECT` subqueries to execute branches only when a condition evaluates to `true`.
81+
`WHEN` can be used inside `COLLECT` subqueries to execute branches conditionally when a predicate evaluates to `true`.
8082
Note that the names and the number of columns returned by the different `WHEN` branches must be identical.
8183
For more information, see xref:queries/composed-queries/conditional-queries.adoc#conditional-subqueries[Conditional queries -> Conditional subqueries].
8284

@@ -94,7 +96,7 @@ RETURN n.name AS name,
9496
ELSE {
9597
RETURN 'Cat owner' AS petStatus
9698
}
97-
} AS petStatus
99+
} AS petStatus
98100
----
99101

100102
[role="queryresult",options="header,footer",cols="2*<m"]

modules/ROOT/pages/subqueries/count.adoc

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,46 @@ RETURN person.name AS name
7171
1+d|Rows: 1
7272
|===
7373

74+
[role=label--new-2025.02]
75+
[[conditional-count]]
76+
== Conditional `COUNT` subquery
77+
78+
`WHEN` can be used inside `COUNT` subqueries to execute branches conditionally when a predicate evaluates to `true`.
79+
Note that the names and the number of columns returned by the different `WHEN` branches must be identical.
80+
For more information, see xref:queries/composed-queries/conditional-queries.adoc#conditional-subqueries[Conditional queries -> Conditional subqueries].
81+
82+
In the example below, the `WHEN` branch is executed if a person has no cat (`c IS NULL`), counting their dogs.
83+
The `ELSE` branch runs for those who have a cat.
84+
85+
.Conditional `COUNT` subquery
86+
[source, cypher]
87+
----
88+
MATCH (p:Person)
89+
OPTIONAL MATCH (p)-[:HAS_CAT]->(c)
90+
RETURN p.name AS person, c IS NOT NULL AS hasCat,
91+
COUNT {
92+
WHEN c IS NULL THEN {
93+
MATCH (p)-[:HAS_DOG]->(dog)
94+
RETURN dog.name AS petOwner
95+
}
96+
ELSE {
97+
MATCH (p)-[:HAS_CAT]->(cat)
98+
RETURN cat.name AS petOwner
99+
}
100+
} AS howManyPets
101+
----
102+
103+
[role="queryresult",options="header,footer",cols="3*<m"]
104+
|===
105+
| name | has Cat | howManyPets
106+
107+
| "Andy" | FALSE | 1
108+
| "Timothy" | TRUE | 1
109+
| "Peter" | FALSE | 2
110+
111+
3+d|Rows: 3
112+
|===
113+
74114

75115
[[count-union]]
76116
== `COUNT` subquery with a `UNION`

modules/ROOT/pages/subqueries/existential.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ RETURN person.name AS name
7777
[[conditional-exists]]
7878
== Conditional `EXISTS` subqueries
7979

80-
`WHEN` can be used inside `EXISTS` subqueries to execute branches only when a condition evaluates to `true`.
80+
`WHEN` can be used inside `EXISTS` subqueries to execute branches conditionally when a predicate evaluates to `true`.
8181
Note that the names and the number of columns returned by the different `WHEN` branches must be identical.
8282
For more information, see xref:queries/composed-queries/conditional-queries.adoc#conditional-subqueries[Conditional queries -> Conditional subqueries].
8383

0 commit comments

Comments
 (0)