neo4j - 返回第一行,不分组

neo4j - returning the first row, without grouping

我正在尝试使用 neo4j 执行以下操作。 我们有具有一组属性的节点。这些属性之一是日期。

我想要 return 编辑这些属性,但只选择最新日期的事件。

在 SQL 中,我的查询将如下所示:

select commonKey, attr1, attr2, dateAttr
from ( 
   select commonKey, attr1, attr2, dateAttr, 
   row_number(
         partition by commonKey 
         order by commonKey ASC, dateAttr DESC
     ) rn
from tab 
) t
WHERE t.rn = 1

有没有办法在 Cypher 中实现同样的功能? 有一个 collect(dateAttr)[1],但这将按我需要 returned (commonKey, attr1, attr2) 的所有属性分组,而我只关心 "group" by commonKey,但是return 其他 - 从组内的第一行开始。

任何 help/suggestions 将不胜感激!

谢谢!

--亚历克斯

我认为您过早地预测属性。按日期排序节点可能会更好,然后 collect() 按日期分组并取第一个:

MATCH (n:MyNode) 
WHERE ...
WITH n, n.dateAttr as dateAttr
ORDER BY dateAttr DESC 
WITH dateAttr, collect(n)[0] as n
RETURN n.commonKey, n.attr1, n.attr2, dateAttr as TDate

或者您可以使用 APOC 程序,它有 aggregation functions 用于这种情况:

MATCH (n:MyNode) 
WHERE ...
WITH n, n.dateAttr as dateAttr
ORDER BY dateAttr DESC 
WITH dateAttr, apoc.agg.first(n) as n
RETURN n.commonKey, n.attr1, n.attr2, dateAttr as TDate