Neo4j - 返回最大值的节点

标签 neo4j cypher

我正在使用以下命令通过 csv 将数据加载到 neo4j

load csv with headers from "file:///path_to.csv" as file
merge (d:DIAGNOSIS {name:file.DIAGNOSIS_SYNONYM})
merge (dn:DRUG_NAME {name:file.DRUG_NAME})
merge (tc:TOTAL_COST {name:toFloat(file.TOTAL_COST)})
merge (cnt:COUNT {name:toInt(file.COUNT)})
merge (ac:AVERAGE_COST {name:toFloat(file.AVERAGE_COST)})
create (dn)-[:for]->(d)
create (d)-[:costs]->(tc)
create (tc)-[:count]->(cnt)
create (cnt)-[:avg_costs]->(ac)

现在我想找到 total_cost 最高/最低的诊断。

我试过了

MATCH ((dn)-[:for]-(d)-[:costs]-(tc)-[:count]-(cnt)-[:avg_costs]->(ac))
WITH d,tc, max(tc.name) as maximum
where tc.name= maximum
return d

但是这会返回所有诊断节点。有人可以建议我做错了什么。

最佳答案

既然您只使用d::DIAGNOSIStc:TOTAL_COST 为什么要MATCH整个路径?我相信您只能 MATCH (d:DIAGNOSIS)-[:costs]-(tc:TOTAL_COST) 并忽略其余部分。

还请记住尽可能使用标签。标签将提高查询的性能。

我相信使用 ORDER BYLIMIT 应该可行。尝试:

MATCH ((d:DIAGNOSIS)-[:costs]-(tc:TOTAL_COST))
RETURN d
ORDER BY tc.name DESC
LIMIT 1

或者,您可以先匹配最大的 tc.name,然后在第二个 MATCH 的 WHERE 中使用它。

MATCH (tc:TOTAL_COST)
WITH  max(tc.name) AS maximum
MATCH ((d:DIAGNOSIS)-[:costs]-(tc:TOTAL_COST))
WHERE tc.name = maximum
RETURN d

关于Neo4j - 返回最大值的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45609435/

相关文章:

neo4j - 有没有一种简单的基于 GUI 的方法来在 neo4j 中构建图形?

如果节点存在,Neo4j 添加/更新属性

neo4j - 密码查询 : Is it possible to "hide" an existing path with a "virtual relationship"?

neo4j - 从 collect 返回一个对象 (cypher/neo4j)

neo4j - 使用 resultDataContents=graph 通过 REST API 返回集合时,为什么排序顺序会丢失

python - py2neo.WriteBatch 如何工作?

Neo4j 在创建节点时向节点添加属性或特性

neo4j - 使用密码获取节点的所有关系

neo4j - 基于时间的图形数据建模

java - 如何从 Neo4j 中的 Node 属性获取实际字符串