Getting the max record with different values using cypher

二次信任 提交于 2019-12-02 05:41:49

I think you are looking for an "arg max" style query. In this case, using collect is the way to go:

MATCH (asset:Asset {name:'Asset Name'})-[]-(ara:AssetRiskAssessment)
WITH asset, ara
CALL spatial.intersects('threat_zones',asset.wkt) YIELD node 
WITH node, asset, ara
MATCH (node)<-[:FOR]-(tss:ThreatScenarioScore)-[]-(ts:ThreatScenario)
WITH node, tss, ts
ORDER BY ts.name ASC, node.zindex DESC
WITH
  ts.name AS name,
  collect({
    zindex: node.zindex, intention: tss.intention, capability: tss.capability
  })[0] AS max
RETURN
  name,
  max.zindex AS zindex,
  max.intention AS intention,
  max.capability AS capability

This sorts the tuples according to their name (ascending), but more importantly, according to their zindex in a descending order. So when the zindex and tss properties are collected to a list, the first item (index [0]) will hold the elements with the maximum zindex value.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!