简体   繁体   English

密码查询:获取按关系属性分组的计数

[英]Cypher query: Get a count grouped by relationship property

I am new to Neo4j/Cypher and I am having some trouble grouping by relationship properties. 我是Neo4j / Cypher的新手,在按关系属性分组时遇到了一些麻烦。

Firstly a toy example: 首先是一个玩具示例:

CREATE (A1:Worker {ref:"A1"})
CREATE (A2:Worker {ref:"A2"})

CREATE (B1:Worker {ref:"B1"})
CREATE (B2:Worker {ref:"B2"})

CREATE (A1)-[:StreamsTo {type:"stream1"}]->(B1)
CREATE (A1)-[:StreamsTo {type:"stream2"}]->(B1)
CREATE (A1)-[:StreamsTo {type:"stream1"}]->(B2)
CREATE (A1)-[:StreamsTo {type:"stream2"}]->(B2)

CREATE (A2)-[:StreamsTo {type:"stream1"}]->(B1)
CREATE (A2)-[:StreamsTo {type:"stream1"}]->(B2)

This creates a graph with 4 worker nodes, where the A nodes are connected to the B nodes by relationships that can have different values for the "type" property. 这将创建一个具有4个工作程序节点的图形,其中A节点通过可以为“ type”属性具有不同值的关系连接到B节点。 In this case A1 is connected to the B's by 2 different types of streams and A2 only by 1 type: 在这种情况下,A1通过2种不同类型的流连接到B,而A2仅通过1种类型连接:

样本图

What I want to be able to do is to count the number of outgoing relationships from each source node but have them grouped by the various values of the "type" property in the relationship to get something like this: 我想要做的是计算每个源节点的传出关系的数量,但是将它们按关系中“ type”属性的各种值进行分组,以获得如下所示的内容:

+--------+-------------+---------------+
| Worker | StreamType  | OutgoingCount |
+--------+-------------+---------------+
| A1     | stream1     | 2             |
+--------+-------------+---------------+
| A1     | stream2     | 2             |
+--------+-------------+---------------+
| A2     | stream1     | 2             |
+--------+-------------+---------------+

So far I can get the total outgoing and number of distinct outgoing types: 到目前为止,我可以得到总传出和不同传出类型的数量:

MATCH (source:Worker)-[st:StreamsTo]->(:Worker)
RETURN source.ref as Source, 
       COUNT(st) as TotalOutgoing, 
       COUNT(distinct st.type) as NumberOfTypes;

Any hints would be helpful. 任何提示都会有所帮助。

So it turns out to be trivial! 因此事实证明这是微不足道的! I had not understood that what you return along with the COUNT() function performs the group by: 我不明白您与COUNT()函数一起返回的内容是通过以下方式进行分组的:

MATCH (source:Worker)-[st:StreamsTo]->(:Worker) 
RETURN source.ref as Worker, 
       st.type as StreamType, 
       COUNT(st) as OutgoingCount;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM