如何计算1:N:1和N:1:N的方式?

huangapple go评论139阅读模式
英文:

How can I count 1:N:1 and N:1:N manners?

问题

只返回翻译好的部分:

我想以1:N:1和N:1:N的方式进行计数。

这是我的查询,但它并没有完全符合我的要求:

匹配
  (V1)-[e1]->(v2)->[e2]->(v3)
与
  计算(distinct(v1.property)) as cntleft,计算(distinct(v3.property)) as cntright
其中
  cntleft=1和cntright=1
返回
  计数(v2)
英文:

I'd like to do a count in the manner of 1:N:1 and N:1:N.

Here is my query, but it is not doing exactly what I want:

Match 
  (V1)-[e1]->(v2)->[e2]->(v3)
With
  Count(distinct(v1.property)) as cntleft , count(distinct(v3.property)) as cntright
Where
 cntleft=1 and cntright=1
Return 
  Count(v2)

答案1

得分: 1

以下是Memgraph中的有效查询:

MATCH (v1)-[e1]-(v2)-[e2]-(v3)
WITH COUNT(DISTINCT v1) AS left, COUNT(DISTINCT v3) as right, v1, v2, v3
WHERE left = 1 AND right = 1
WITH collect(v2) AS n, v1, v3
RETURN COUNT(*);

首先,我们要注意关系(->)。这种结构不被支持:

(v2)->[e2]-(v3)

因为足够使用 (v2)-[e2]-(v3)

然后,如你在上面的查询中所看到的,DISTINCT的使用是不带括号的,而且你不需要使用特定属性来计算你想要的内容。只需要计算不同的节点。

另一件事是不要忘记连接 v1v2v3,因为我们后面会需要它们。

我认为左边和右边等于1的部分是正确的,最后,我收集了这些类型的 v2 来计算所有的 1:N:1,而不是 1:v2:11:v2:1,最终的结果。要查看你获得的关系,你可以运行以下查询:

MATCH (v1)-[e1]-(v2)-[e2]-(v3)
WITH COUNT(DISTINCT v1) AS left, COUNT(DISTINCT v3) as right, v1, v2, v3
WHERE left = 1 AND right = 1
RETURN v1, collect(v2) AS n, v3;

类似地,对于 N:1:N,我会运行以下查询:

MATCH (v1)-[e1]-(v2)-[e2]-(v3)
WITH COUNT(DISTINCT v2) AS middle, v1, v2, v3
WHERE middle = 1
WITH collect(v1) AS n1, v2, collect(v3) AS n2
RETURN COUNT(*);

这样,你可以发现这些节点:

MATCH (v1)-[e1]-(v2)-[e2]-(v3)
WITH COUNT(DISTINCT v2) AS middle, v1, v2, v3
WHERE middle = 1
RETURN collect(v1), v2, collect(v3);
英文:

Here is a working query in Memgraph:

MATCH (v1)-[e1]->(v2)-[e2]->(v3)
WITH COUNT(DISTINCT v1) AS left, COUNT(DISTINCT v3) as right, v1, v2, v3
WHERE left = 1 AND right = 1
WITH collect(v2) AS n, v1, v3
RETURN COUNT(*);

First, we have to be careful with relationships (->). This kind of construct is not supported:

(v2)->[e2]->(v3)

because it is enough to put (v2)-[e2]->(v3).

Then, as you can see in the above query, the usage of DISTINCT is without the parenthesis, and you don't have to use a certain property to count what you wanted. It is enough to count the distinct nodes.

The other this you have to do is not to forget to pipe v1, v2, and v3, since we need them later on.
I think this part with left and right = 1 is fine, and in the end, I collected these kind of v2's to count all 1:N:1, instead of 1:v2:1, 1:v2:1,..as a result. To see the relationships you got, you can run:

MATCH (v1)-[e1]->(v2)-[e2]->(v3)
WITH COUNT(DISTINCT v1) AS left, COUNT(DISTINCT v3) as right, v1, v2, v3
WHERE left = 1 AND right = 1
RETURN v1, collect(v2) AS n, v3;

Similarly, for N:1:N, I would run:

MATCH (v1)-[e1]->(v2)-[e2]->(v3)
WITH COUNT(DISTINCT v2) AS middle, v1, v2, v3
WHERE middle = 1
WITH collect(v1) AS n1, v2, collect(v3) AS n2
RETURN COUNT(*);

That is, to discover the nodes:

MATCH (v1)-[e1]->(v2)-[e2]->(v3)
WITH COUNT(DISTINCT v2) AS middle, v1, v2, v3
WHERE middle = 1
RETURN collect(v1), v2, collect(v3);

huangapple
  • 本文由 发表于 2023年3月3日 23:28:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定