Neo4j查询:应用多个WHERE条件无效

z0qdvdin  于 2022-11-05  发布在  其他
关注(0)|答案(2)|浏览(231)

我有一个运行良好的查询。这给了我所有当前fuelPercentLeft超过50%的列车。

MATCH (t:Train) 
WITH t.trainId AS trainid, MAX(t.timestamp) AS maxTimeStamp
MATCH (n:Train{trainId: trainid, timestamp: maxTimeStamp})-[:HAS]->(m:Attributes{name:'fuelPercentLeft'}) 
WHERE toInteger(m.value) > 50
RETURN count(toInteger(m.value))

我只想用一个查询返回多个结果。例如,对于所有的列车,返回fuelPercentLeft大于50、Engine Temperature小于20、Lubricant Level小于10的所有列车。在上面的查询中,是否可以应用多个WHERE条件?我们是否可以执行以下操作?

MATCH (t:Train) 
WITH t.trainId AS trainid, MAX(t.timestamp) AS maxTimeStamp
CALL {
MATCH (n:Train{trainId: trainid, timestamp: maxTimeStamp})-[:HAS]->(m:Attributes{name:'fuelPercentLeft'}) 
WHERE toInteger(m.value) > 50
RETURN count(toInteger(m.value)) AS goodFuel
}
CALL {
MATCH (n:Train{trainId: trainid, timestamp: maxTimeStamp})-[:HAS]->(m:Attributes{name:'fuelPercentLeft'}) 
WHERE toInteger(m.value) < 50
RETURN count(toInteger(m.value)) AS badFuel
}
CALL {
MATCH (n:Train{trainId: trainid, timestamp: maxTimeStamp})-[:HAS]->(m:Attributes{name:'engineTemp'}) 
WHERE toInteger(m.value) > 20
RETURN count(toInteger(m.value)) AS goodEngine
}
CALL {
MATCH (n:Train{trainId: trainid, timestamp: maxTimeStamp})-[:HAS]->(m:Attributes{name:'lubricantLevel'}) 
WHERE toInteger(m.value) < 10
RETURN count(toInteger(m.value)) AS badLubricant
}

RETURN goodFuel, badFuel, goodEngine, badLubricant

我试过这个,但它不像预期的那样工作。要求是,一个单一的查询应该能够基于多个条件返回多个值。
请帮忙。谢谢!

ztyzrc3y

ztyzrc3y1#

您可以在链中使用可选匹配:

MATCH (t:Train) 
WITH t.trainId AS trainid, MAX(t.timestamp) AS maxTimeStamp

OPTIONAL 
MATCH (n:Train{trainId: trainid, timestamp: maxTimeStamp})-[:HAS]->(m:Attributes{name:'fuelPercentLeft'}) 
WHERE toInteger(m.value) > 50

WITH trainid, maxTimeStamp, count(m) AS goodFuel

OPTIONAL 
MATCH (n:Train{trainId: trainid, timestamp: maxTimeStamp})-[:HAS]->(m:Attributes{name:'fuelPercentLeft'}) 
WHERE toInteger(m.value) < 50

WITH trainid, maxTimeStamp, goodFuel, count(m) AS badFuel

OPTIONAL 
MATCH (n:Train{trainId: trainid, timestamp: maxTimeStamp})-[:HAS]->(m:Attributes{name:'engineTemp'}) 
WHERE toInteger(m.value) > 20

WITH trainid, maxTimeStamp, goodFuel, badFuel, count(m) AS goodEngine

OPTIONAL 
MATCH (n:Train{trainId: trainid, timestamp: maxTimeStamp})-[:HAS]->(m:Attributes{name:'lubricantLevel'}) 
WHERE toInteger(m.value) < 10

WITH trainid, maxTimeStamp, goodFuel, badFuel, goodEngine, count(m) AS badLubricant

RETURN trainid, maxTimeStamp, goodFuel, badFuel, goodEngine, badLubricant
5sxhfpxr

5sxhfpxr2#

您应用了错误的筛选器,对于trainIdmaxTimeStamp,它们不是必需的,请尝试以下操作:

MATCH (n:Train)-[:HAS]->(m:Attributes{name:'fuelPercentLeft'}) 
WHERE toInteger(m.value) > 50
WITH count(n) AS goodFuel
MATCH (n:Train)-[:HAS]->(m:Attributes{name:'fuelPercentLeft'}) 
WHERE toInteger(m.value) < 50
WITH goodFuel, count(n) AS badFuel
MATCH (n:Train)-[:HAS]->(m:Attributes{name:'engineTemp'}) 
WHERE toInteger(m.value) > 20
WITH goodFuel, badFuel, count(toInteger(m.value)) AS goodEngine
MATCH (n:Train)-[:HAS]->(m:Attributes{name:'lubricantLevel'}) 
WHERE toInteger(m.value) < 10
RETURN count(toInteger(m.value)) AS badLubricant, goodFuel, badFuel, goodEngine

相关问题