这与此问题有关:How to store properties of a neo4j node as an array? 我想迭代关系的属性并检查该值的最大值并分配node1和node2的新关系,并从池中删除node1并移至第二个。换句话说,就像我上一个问题的背景一样,如何根据max(r.score)将给定员工分配到给定职位,并转移到另一个具有最大r.score的员工?感谢
让这个基本查询为具有最大r.score w.r.t位置的员工分配一个职位,并将他从候选人库中删除。但是,我必须手动运行第二个位置。理想情况下,我想要一些东西,如果可用的位置检查长度,然后用max(r.score)填充位置,然后在所有位置都填满时停止。可能会返回雇佣员工的报告......
MATCH (e:Employee)-[r:FUTURE_POSITION]->(p:Position)
WITH MAX(r.score) as s
MATCH (e)-[r]->(p) WHERE r.score = s
CREATE (e)-[r2:YOUAREHIRED]->(p)
DELETE r
RETURN e.name, s
答案 0 :(得分:0)
此查询可能对您有用:
MATCH (:Employee)-[r:FUTURE_POSITION]->(p:Position)
WITH p, COLLECT(r) AS rs
WITH p, REDUCE(t = rs[0], x IN rs[1..] |
CASE WHEN x.score > t.score THEN x ELSE t END) AS maxR
WITH p, maxR, maxR.score AS maxScore, STARTNODE(maxR) AS e
CREATE (e)-[:YOUAREHIRED]->(p)
DELETE maxR
RETURN p, e.name AS name, maxScore;
WITH
子句收集每个FUTURE_POSITION
的所有p
关系。WITH
子句为每个p
获取具有最大score
的关系。WITH
子句提取后续子句所需的变量。CREATE
子句在YOUAREHIRED
(给定e
得分最高的员工)和p
之间创建p
关系。DELETE
子句删除FUTURE_POSITION
和e
之间的p
关系。RETURN
子句返回每个p
,以及刚被name
雇用的雇员的p
和他的得分maxScore
。[更新]
如果您要删除获得FUTURE_POSITION
关系的每个p
节点的全部 YOUAREHIRED
关系,则可以使用以下稍微不同的查询:>
MATCH (:Employee)-[r:FUTURE_POSITION]->(p:Position)
WITH p, COLLECT(r) AS rs
WITH p, rs, REDUCE(t = rs[0], x IN rs[1..] |
CASE WHEN x.score > t.score THEN x ELSE t END) AS maxR
WITH p, rs, maxR.score AS maxScore, STARTNODE(maxR) AS e
CREATE (e)-[:YOUAREHIRED]->(p)
FOREACH(x IN rs | DELETE x)
RETURN p, e.name AS name, maxScore;