Getting the max record with different values using cypher

后端 未结 1 1621
没有蜡笔的小新
没有蜡笔的小新 2021-01-25 00:22

I have a graph with spatial data using the spatial plugin.

This graph has \"Threat Zones\" (Polygons) which can be on top of the other so they also have a z-index proper

相关标签:
1条回答
  • 2021-01-25 00:34

    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.

    0 讨论(0)
提交回复
热议问题