neo4j-返回第一行,不分组
我正试图用neo4j做以下工作。 我们有具有一组属性的节点。其中一个属性是日期 我希望返回这些属性,但只获取最新日期的事件 在SQL中,我的查询如下所示:neo4j-返回第一行,不分组,neo4j,group-by,row,limit,Neo4j,Group By,Row,Limit,我正试图用neo4j做以下工作。 我们有具有一组属性的节点。其中一个属性是日期 我希望返回这些属性,但只获取最新日期的事件 在SQL中,我的查询如下所示: select commonKey, attr1, attr2, dateAttr from ( select commonKey, attr1, attr2, dateAttr, row_number( partition by commonKey order by commonKey A
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],但这将根据我需要返回的所有属性(commonKey、attr1、attr2)进行分组,而我只关心按commonKey“分组”,但从组中的第一行返回其他属性
如有任何帮助/建议,将不胜感激
谢谢大家!
--亚历克斯我认为你过早地预测了房地产。按日期对节点进行排序,然后按日期对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程序,该程序针对此类情况:
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
你能提供到目前为止你已经尝试过的密码吗?类似这样的东西:按commonKey匹配(p)xxx顺序,用commonKey匹配dateAttr DESC,attr1,attr2,collect(dateAttr)[1]作为TDate返回commonKey,attr1,attr2,TDate