我在Neo4j图中有以下数据dB:
Year Fruit Count
2006 Apple 10
2006 Mango 15
2006 Banana 30
2007 Apple 13
2007 Mango 11
2007 Banana 27
2008 Apple 2
2008 Mango 23
2008 Banana 47
我有兴趣获得以下结果:
Year Apple Banana Mango
2006 10 30 15
2007 13 27 11
2008 2 47 23
如何使用Neo4j Cypher查询获得此结果?
答案 0 :(得分:3)
与SQL类似,结果列是固定的(除非您使用RETURN year, { banana: case fruit when "banana" then count else null end,
apple: case fruit when "apple" then count else null end,
mango: case fruit when "mango" then count else null end} as data
)。
你最接近的是创建一张地图并返回地图。
RETURN year, apoc.map.fromPairs(collect([fruit, count])) as data
使用apoc library可以概括:
In [90]: (df[df.measure!=3]
.groupby(['H#','measure'], as_index=False)
.sum()
.append(df.loc[df.measure==3]))
Out[90]:
H# measure D N
0 12843 1 3 6
1 20000 2 2 2
3 12843 3 1 4
4 20000 3 1 5
5 20000 3 1 0
6 20000 3 2 0