首页 > 解决方案 > 将输出作为数组而不是在 neo4j 中的单独行中

问题描述

我写了一个查询,它返回四列,四列在单独的行中有值。

但我想要的是查询应该为我返回一个不同列的数组,而不是单独行中的值。

查询是:

MATCH(c:Company{companyId:4})<-[re:WORKS_IN]-(p:Person)-[r:HAS_SCORE]->(ps:PerformanceScores)
WITH distinct ps.scoreDate as scoredate
MATCH (ps:PerformanceScores {scoreDate:scoredate})
WITH ps.score as score, scoredate
UNWIND scoredate as x
WITH x,score,scoredate
RETURN avg(score)as average,x as fulldate,["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"][scoredate.month-1] as months,scoredate.year as year order by x

那么查询中应该改变什么,以便它返回数组中的值。

谢谢

标签: neo4jcypher

解决方案


通常,当您想要数组中的每个结果列时,此方法有效:

MATCH (p:Person) 
RETURN COLLECT(id(p)) AS ids,
       COLLECT(p.name) AS names

对于您的查询,这看起来像

MATCH(c:Company{companyId:4})<-[re:WORKS_IN]-(p:Person)-[r:HAS_SCORE]->(ps:PerformanceScores) 
WITH distinct ps.scoreDate as scoredate 
MATCH (ps:PerformanceScores {scoreDate:scoredate}) 
WITH ps.score as score, scoredate 
UNWIND scoredate as x 
WITH x,score,scoredate 
WITH { average:avg(score),
   fulldate: x,
   months:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"][scoredate.month-1],
   year:scoredate.year 
   } as row order by row.fulldate
RETURN COLLECT (average) as averages, 
 ... etc

推荐阅读