where子句中具有聚合的复杂查询 [英] Complex query with agregation in where clause

查看:167
本文介绍了where子句中具有聚合的复杂查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个图,其中对于BallT类型的每对节点m,n,可以有一个BallD类型的节点k,它通过Rel类型的关系将它们连接起来,也就是说,可以有多种模式种类p=(m:BallT)-[r:Rel]-(k:BallD)-[s:Rel]-(n:BallT).对于给定的节点m(例如满足m.key="whatever"的要求),我们将连接m的BallD的数目称为Nmn,将某个节点n和N的总数称为BallD节点的总数.对于这个给定的节点m,我如何找到所有节点n使得Nmn> N/2并按Nnm排序结果?我正在尝试查询:

Suppose that i have a graph in which for each pair of nodes m,n of type BallT there can be a node k of type BallD that connects them through relationships of type Rel, that is, there can be multiple patterns of the kind p=(m:BallT)-[r:Rel]-(k:BallD)-[s:Rel]-(n:BallT). For a given node m (satisfying for example m.key="whatever") let's call Nmn the number of BallD connecting m and some node n and N the total number of BallD nodes. For this given node m how can i found all nodes n such that Nmn > N/2 and order the results by Nnm? I'm trying the query:

match (D:BallD)
with count(D) as N
match (m:BallT {key:"whatever"})-[r]-(d:BallD)-[s]-(n:BallT)
with N, distinct n as n_dist, count(d) as Nmn
where Nmn >= N
return n_dist
order by Nmn

但是我要得到

Invalid input 't': expected whitespace, comment, node labels, MapLiteral, a parameter, a relationship pattern

推荐答案

此查询是否可以满足您的要求?

Does this query do what you want?

MATCH (D:BallD)
WITH count(D) as N
MATCH (m:BallT {key:"whatever"})--(d:BallD)--(n:BallT)
WITH N, n, count(d) as Nmn
WHERE Nmn >= N/2
RETURN n, Nmn
ORDER BY Nmn

  • 在某个值(例如n)上使用聚合函数(例如count)时,不需要使用DISTINCT限定要聚合的值-这样做自动为您服务.

    • When using an aggregation function (like count) over a value (like n), you do not need to use DISTINCT to qualify the value that you are aggregating over -- that is done automatically for you.

      ORDER BY参数必须引用返回值的一部分.

      The ORDER BY argument must refer to a part of the returned values.

      此外,我更正了WHERE子句以使用N/2.

      Also, I corrected the WHERE clause to use N/2.

      这篇关于where子句中具有聚合的复杂查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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