问题描述
有关
我现在需要创建车站与附近(LOCATED_IN)之间的关系.第一个查询每个站仅成功返回1行(1个组合).我要在其中创建关系的第二个查询创建了太多的关系.
I now need to create the relationship between a station and a neighborhood (LOCATED_IN). The first query successfully returns only 1 row per station (1 combination). The second query where I want to create the relationship, creates too much relationships.
MATCH (n:Neighborhood),(s:Station)
WITH n, s, distance(n.centerLocation, s.point) AS dist
ORDER BY dist
RETURN s.stationId, COLLECT(n)[0].name AS name, COLLECT(dist)[0] AS shortest
ORDER BY s.stationId
查询2:
MATCH (n:Neighborhood),(s:Station)
WITH n, s, distance(n.centerLocation, s.point) AS dist
ORDER BY dist
CREATE (s)-[r:LOCATED_IN]->(nbh)
RETURN s.stationId, COLLECT(n)[0] AS nbh, COLLECT(dist)[0] AS shortest
ORDER BY s.stationId
查询3:
MATCH (n:Neighborhood),(s:Station)
WITH n, s, distance(n.centerLocation, s.point) AS dist
ORDER BY dist
CREATE (s)-[r:LOCATED_IN]->(n)
RETURN s.stationId, COLLECT(n)[0] AS nbh, COLLECT(dist)[0] AS shortest
ORDER BY s.stationId
查询2之后:工作站(蓝色节点)连接到不代表邻居节点的多个节点.他们只有一个ID.
After query 2:Stations (blue nodes) are connected to multiple nodes that do not represent neighborhood nodes. They only have an ID.
查询3之后:站点(蓝色节点)连接到多个邻域节点.每个站点只能是一个邻居.
After query 3:Stations (blue nodes) are connected to multiple neighborhoods nodes. It should only be one neighborhood per station.
我应该如何编写查询以使每个站点有1个邻居?
推荐答案
查询2存在缺陷,因为CREATE
使用的是未绑定的nbh
变量.
Query 2 is flawed because the CREATE
is using an unbound nbh
variable.
对于查询3,您需要创建从每个测站到仅一个最近邻域的关系.例如:
For Query 3, you need to create a relationship from each station to just the single nearest neighborhood. For example:
MATCH (n:Neighborhood), (s:Station)
WITH n, s, distance(n.centerLocation, s.point) AS dist
ORDER BY dist
WITH s, COLLECT(dist)[0] AS shortest, COLLECT(n)[0] AS nbh
CREATE (s)-[:LOCATED_IN]->(nbh)
RETURN s.stationId, nbh, shortest
ORDER BY s.stationId
这篇关于与聚合和多个order by子句匹配后创建关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!