如何在集合上使用max()? [英] How to use max() on a collection?

查看:99
本文介绍了如何在集合上使用max()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的数据集:

I have a dataset that looks something like this:

CREATE (n {name:'main', val:3}) -[:r]-> ({name:'sub1', val:2}), (n)-[:r]->({name:'sub2', val:1})

现在,我需要找到连接到名为"main"的节点(也包括"main")的所有节点的"val"的最大值.因此,在这种情况下,答案是 3.

Now, I need to find the maximum value for 'val' for all nodes that are connected to the node named 'main' (including 'main' too). So, in this case the answer is 3.

由于名为"main"的节点可能没有任何子节点,因此我使用OPTIONAL MATCH查找子节点,然后将找到的所有val合并到一个列表中,并在其上调用max(),就像这样:

Since the node named 'main' may not have any subnodes, I used OPTIONAL MATCH to find the subnodes, then combine all the vals found into a list and call max() on it, like so:

MATCH (n {name:'main'})
OPTIONAL MATCH (n)-[:r]->(subs)
RETURN max(n.val +  collect(subs.val))

但这会产生以下错误:

类型不匹配:预期为Float或Integer,但为Collection(第 3,第18列(偏移量:73))返回最大(n.val +收集(subs.val))"

Type mismatch: expected Float or Integer but was Collection (line 3, column 18 (offset: 73)) "RETURN max(n.val + collect(subs.val))"

解决这种问题的正确方法是什么?

What is the correct may to solve this sort of problem?

推荐答案

这应该对您有用:

MATCH p=(n {name:'main'})-[:r*0..]->(subs)
UNWIND NODES(p) AS node
RETURN MAX(node.val) AS result;

此查询使用可变长度模式来跟踪整个内容(可选)r关系链.您的查询没有. NODES()函数生成路径节点的集合,而UNWIND将集合转换为数据行.像MAX()这样的汇总功能只能在数据行之间汇总.

This query uses a variable-length pattern to follow the entire (optional) chain of r relationships. Your query did not. The NODES() function produces a collection of path nodes, and the UNWIND converts the collection to data rows. Aggregation functions like MAX() only aggregate across data rows.

这篇关于如何在集合上使用max()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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