No worries, I am also learning neo4j. I wrote an application using a relational DBMS, but naturally fits a graph database, so I got excited when I learned of neo4j. I am in the process of replatforming it.
Cypher basically pattern matches. Each result row is a match of your pattern. In your case, your match statement below finds two paths that match.
match p=((a:label {Name:'Pro7'})-[*4]->(b:label {Name:'Pro1'}))
The Neo4j Browser view of the result shows the matching nodes and renders them with all the relationships, so it looks like the result is a subgraph. The actual results of the query are shown under the text or table views. As you can see below, the result consist of two paths.
It is easy to count the number of relationships ingoing/outgoing from a node with the query below:
match(n{key:value})
match(n)-(c)
return count(c)
In your case, you wanted to restrict the relationship count to only those relationships that are part of your query results. Assuming allSubGraphNodes is a list of those nodes resulting from your query, you can modify the above query to restrict the count to just those relationships between the allSubGraphNodes:
match(n{key:value})
match(n)-(c)
where (c in allSubGraphNodes)
return count(c)
To get this to work for your case, we need to take the query results that consists of two separate paths, and convert them into a single list of all the nodes along either path. That is the goal of the following part of the query:
match p=((a:label {Name:'Pro7'})-[*4]->(b:label {Name:'Pro1'}))
with reduce(s=, i in collect(nodes(p)) | i + s) as allSubGraphNodes
To process the list of allSubGraphNodes through the query part that counts the relationships per node, we need to convert the list into rows using the UNWIND clause, then pass the rows and the list of allSubGraphNodes to the next phase of the query using the WITH clause. The DISTINCT clause removes the duplicate nodes that are in multiple paths. That is what this part of the query does:
unwind allSubGraphNodes as node
with distinct node, allSubGraphNodes
To answer your question specifically, it is the WITH clause that allows you to chain query results to form a query pipeline to formulate the final result you want .
You can also execute subqueries using the CALL clause. This allows you to execute a full cypher query block for each row of the outer query's result. The subquery's result will be added to the outer query's result. The subquery can result in zero, one, or N results. If zero, the query will stop. If one, then the one result will be added to the outer query result. If N, then each subquery result will be added to the outer query's result N times. This is done on a per outer query result row. BTW- the subquery can be either to perform more querying to augment the outer query (similar to a join in SQL), or it can be to perform a mutation operation, such as creating new nodes based on the outer query result.
As discussed, cypher results in collections of paths for its results. If this doesn't work for your requirements and you want to work with subgraphs, then you may consider two alternatives. First, you can use one of the client drivers to iteratively walk the subgraph to formulate your subgraph for additional processing. You would do this by getting your root node, then get the child nodes through the root node's relationships. You can continue this process iteratively until you reach the terminal nodes of your subgraph. You can program an iterative algorithm and execute it in one ReadTransaction.
If this is something you do often and performance is an issue, you can consider writing a custom procedure using the neo4j Java API to run the algorithm on the server itself. You would then call the procedure in cypher. For example, you would match the root node in cypher and in the same query, send that node to your procedure and get back your processed subgraph. I have done this for a project I am working on, so if you want to get jumpstarted I would be happy to help. It took me a while to wade through the documentation and get it working. I now have a nice working platform to development and unit test.