密码查询:获取按关系属性分组的计数 [英] Cypher query: Get a count grouped by relationship property

查看:93
本文介绍了密码查询:获取按关系属性分组的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Neo4j/Cypher的新手,在按关系属性分组时遇到了一些麻烦.

I am new to Neo4j/Cypher and I am having some trouble grouping by relationship properties.

首先是一个玩具示例:

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)

这将创建一个具有4个工作程序节点的图形,其中A节点通过可以为"type"属性具有不同值的关系连接到B节点.在这种情况下,A1通过2种不同类型的流连接到B,而A2仅通过1种类型连接:

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. In this case A1 is connected to the B's by 2 different types of streams and A2 only by 1 type:

我想要做的是计算每个源节点的传出关系的数量,但是将它们按关系中"type"属性的各种值进行分组,以得到如下信息:

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:

+--------+-------------+---------------+
| 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.

推荐答案

事实证明这很简单!我不了解您将COUNT()函数返回的内容与分组依据是:

So it turns out to be trivial! I had not understood that what you return along with the COUNT() function performs the group by:

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

这篇关于密码查询:获取按关系属性分组的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆