问题描述
这与以下问题有关:如何将neo4j节点的属性存储为数组?我想遍历关系的一个属性,并检查该值的最大值,并分配新的node1和node2关系,并从池中删除node1并移至第二个.换句话说,就像在我之前的问题中一样,如何将给定员工分配给基于max(r.score)的给定职位,并转移到另一个为另一个职位拥有最大r.score的员工?谢谢
This is related to this question: How to store properties of a neo4j node as an array?I would like to iterate through a property of a relationship and check max of that value and assign a new relationship of node1 and node2 and delete node1 from the pool and move to the second one. In other words as in the context of my previous question, How to assign a given employee to a given position based max(r.score) and move to the other employee who has a maximum r.score for another position? Thanks
具有此基本查询可为具有最高r.score w.r.t职位的员工分配职位,并将其从候选人池中删除.但是,我必须手动将其运行到第二个位置.理想情况下,我需要一种检查长度(如果有)的长度,然后用max(r.score)填充位置,然后在所有位置填充后停止的东西.可能会返回聘用员工的报告...
Have this basic query to assign a position for the employee who has a maximum r.score w.r.t position and remove him from pool of candidates. However, I have to run this manually for the second position. Ideally I want something that checks length if available positions and then fills positions with max(r.score) and then stops when all positions are filled. may be returns a report of hired employees...
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
推荐答案
此查询可能对您有用:
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
子句收集每个p
的所有FUTURE_POSITION
关系. - 第二个
WITH
子句为每个p
获取与最大score
的关系. - 第三个
WITH
子句提取后续子句所需的变量. -
CREATE
子句在e
(给定p
的得分最高的员工)和p
之间创建YOUAREHIRED
关系. -
DELETE
子句删除e
和p
之间的FUTURE_POSITION
关系. -
RETURN
子句返回每个p
,以及刚为p
雇用的员工的name
和他的分数maxScore
. - The first
WITH
clause collects all theFUTURE_POSITION
relationships for eachp
. - The second
WITH
clause obtains, for eachp
, the relationship with the maximumscore
. - The third
WITH
clause extracts the variables needed by subsequent clauses. - The
CREATE
clause creates theYOUAREHIRED
relationship betweene
(the employee with the highest score for a givenp
) andp
. - The
DELETE
clause deletes theFUTURE_POSITION
relationship betweene
andp
. - The
RETURN
clause returns eachp
, along with and thename
of the employee who was just hired forp
, and his score,maxScore
.
[更新]
如果要删除每个获得YOUAREHIRED
关系的p
节点的 all FUTURE_POSITION
关系,则可以使用以下稍微不同的查询:
If you want to delete all FUTURE_POSITION
relationships of each p
node that gets a YOUAREHIRED
relationship, you can use this slightly different query:
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;
这篇关于是否可以通过关系密码的属性进行迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!