CASE何时返回Cypher的最大边缘属性

时间:2017-11-08 12:41:24

标签: neo4j cypher

我试图找出如何在查询中使用条件。基本上,在下面的教师和学校之间的关系中,我只需要获得从一所学校搬到另一所学校的教师。为了做到这一点,我在[:HAD]的where语句中使用了属性(从1到4,1是最低角色,4是最高),但查询带来的老师也在同一所学校内升职 - 有多个[ :HAD],我不需要。所以我想介绍一个if语句,他说如果教师和学校之间有多个[:HAD],那么只带来最大的[:HAD]属性。

这是原始代码,它将在同一所学校升职的教师以及晋升到另一所学校的教师带来:

//CREATE LINK BETWEEN SCHOOLS FOR CENTRALITY MEASURE
//BASED ON TEACHERS MOVING TO ANOTHER SCHOOL BASED ON PROMOTION
MATCH (s1:School)<-[:WITH]-(:Contract)<-[x:HAD]-(:Teacher)-[y:HAD]->(:Contract)-[:WITH]->(s2:School)
WHERE y.HAD>x.HAD and s1 <> s2
MERGE (s1)-[:TRANSFER_ON_PROMOTION]->(s2)
RETURN s1, s2

这是我对原始代码的更改:

//CREATE LINK BETWEEN SCHOOLS FOR CENTRALITY MEASURE
//BASED ON TEACHERS MOVING TO ANOTHER SCHOOL BASED ON PROMOTION
MATCH (s1:School)<-[:WITH]-()<-[x:HAD]-(:Teacher)-[y:HAD]->()-[:WITH]->(s2:School)
WHERE y.HAD>x.HAD and s1 <> s2
RETURN toInteger(x.HAD) AS x,
CASE 
    WHEN (x ORDER BY x DESC LIMIT 1) > 1 THEN x
    ELSE 1
END as highest
MATCH (s1:School)<-[:WITH]-(:Contract)<-[x:HAD]-(:Teacher)-[y:HAD]->(:Contract)-[:WITH]->(s2:School)
WHERE y.HAD>highest and s1 <> s2
MERGE (s1)-[:TRANSFER_ON_PROMOTION]->(s2)
RETURN s1, s2

1 个答案:

答案 0 :(得分:0)

您可以使用WITH链接查询,max选择最大HAD值:

MATCH (s1:School)<-[:WITH]-(:Contract)<-[x:HAD]-(:Teacher)-[y:HAD]->(:Contract)-[:WITH]->(s2:School)
WHERE s1 <> s2
WITH s1, max(x.HAD) AS xMaxHad, y, s2
WHERE y.HAD > xMaxHad
MERGE (s1)-[:TRANSFER_ON_PROMOTION]->(s2)
RETURN s1, s2

请注意,建模方面,为HAD关系添加HAD属性似乎是不好的做法 - 我建议改为使用不同的属性名称(例如{{1} }或level)。即使rank不是一个非常好的名字,也许HAD会更好。