【问题标题】:Getting the top 10 values of a property in Neo4j cypher在 Neo4j 密码中获取属性的前 10 个值
【发布时间】:2021-08-23 20:00:14
【问题描述】:

我的图表上有一个节点 N1。该节点具有多个属性。我对属性 P1 和 P2 感兴趣。对于每个属性 P1,P2 可以有多个“行”。我想为节点 N1 中的每个 P1 提取前 10 个 P2。 我尝试了以下方法:

match (m:N1)
where m.P1 is not null

with m
match(n:N1{P1:m.P1})
where n.P2 is not null
return 
m.P1 as P1_test, n.P2 as P2_test, count(*) as testCount
order by testCount desc
limit 10

以上没有给我正确的价值观。 我还尝试了以下方法:

match (n:N1) where n.P1 is not null
with n.P1 as P1_test
match (m:N1{P1:P1_test})
where m.P2 is not null
return
m.P1,
collect (m.P2) as P2_test

这不起作用,我无法在此处添加计数以限制查询的前 10 个结果。 我不确定我是否在这里遗漏了一些基本的东西。非常感谢您在正确方向上的任何帮助。

【问题讨论】:

    标签: group-by neo4j cypher


    【解决方案1】:
    • 您可以执行一次匹配查询来检查 P1 和 P2 是否不为空。
    • 然后将结果按 P1 和 P2 的个数降序排列。
    • 收集 P2 及其计数,然后获取前 10 行
    • 展开就像一个 for 循环
    • 最后,返回 P1 和 P2

    将来,也请给我们提供样本数据以供我们处理。谢谢。

    match (n:N1) where n.P1 is not null and n.P2 is not null
    with n.P1 as P1_test, n.P2 as P2_test, count(n.P2) as cnt order by P1_test, cnt desc
    with P1_test,  collect({p2: P2_test, cnt:cnt})[..10] as p2_list
    unwind p2_list as c
    with P1_test, c.p2 as P2_test
    return P1_test, P2_test
    

    【讨论】:

      【解决方案2】:

      在 WHERE 之后,您可以这样做:

      WITH
      m.P1 AS P1_test, n.P2 AS P2_test, count(*) AS testCount
      ORDER BY testCount DESC
      WITH P1_test, collect(P2_test) AS P2s
      RETURN P1_test, P2s[..10] AS top10
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-18
        • 1970-01-01
        • 1970-01-01
        • 2012-11-20
        相关资源
        最近更新 更多