Databases Reference
In-Depth Information
As we've seen in the previous queries, the simplest thing we can do is return the plays
we've found:
RETURN DISTINCT play.title AS play
DISTINCT ensures we return unique results. Because each play can be performed mul‐
tiple times in the same theater, sometimes in different productions, we can end up with
duplicate play titles. DISTINCT filters these out.
We can enrich this result in several ways, including aggregating, ordering, filtering, and
limiting the returned data. For example, if we're only interested in the number of plays
that match our criteria, we apply the count function:
RETURN count (play)
If we want to rank the plays by the number of performances, we'll need first to bind the
PERFORMANCE_OF relationship in the MATCH clause to an identifier, called p , which we can
then count and order:
START theater= node :venue(name= 'Theatre Royal' ),
newcastle= node :city(name= 'Newcastle' ),
bard= node :author(lastname= 'Shakespeare' )
MATCH (newcastle)<-[:STREET|CITY*1..2]-(theater)
<-[:VENUE]-()-[p:PERFORMANCE_OF]->()-[:PRODUCTION_OF]->
(play)<-[:WROTE_PLAY]-(bard)
RETURN play.title AS play, count (p) AS performance_count
ORDER BY performance_count DESC
The RETURN clause here counts the number of PERFORMANCE_OF relationships using the
identifier p (which is bound to the PERFORMANCE_OF relationships in the MATCH clause)
and aliases the result as performance_count . It then orders the results based on per
formance_count , with the most frequently performed play listed first:
+-------------------------------------+
| play | performance_count |
+-------------------------------------+
| "Julius Caesar" | 2 |
| "The Tempest" | 1 |
+-------------------------------------+
2 rows
Query Chaining
Before we leave our brief tour of Cypher, there is one more feature that it is useful to
know about—the WITH clause. Sometimes it's just not practical (or possible) to do
everything you want in a single MATCH . The WITH clause allows us to chain together several
matches, with the results of the previous query part being piped into the next. In the
following example we find the plays written by Shakespeare, and order them based on
Search WWH ::




Custom Search