Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

How to sum all values in a returned column Cypher?

I have written the following Cypher query.

MATCH (c:Customers {name:'Paul Pogba'})-[:ORDERED]->(o:Orders)-[:BOUGHT]->(i:Items) Return c, i.kit, count(i)

I get a table like this as my output.

╒═════════════════════╤═════════════╤══════════╕
│"c"                  │"i.kit"      │"count(i)"│
╞═════════════════════╪═════════════╪══════════╡
│{"name":"Paul Pogba"}│"bumper"     │1         │
├─────────────────────┼─────────────┼──────────┤
│{"name":"Paul Pogba"}│"wing mirror"│1         │
├─────────────────────┼─────────────┼──────────┤
│{"name":"Paul Pogba"}│"bonnet"     │1         │
├─────────────────────┼─────────────┼──────────┤
│{"name":"Paul Pogba"}│"boot"       │2         │
├─────────────────────┼─────────────┼──────────┤
│{"name":"Paul Pogba"}│"wheel"      │1         │
├─────────────────────┼─────────────┼──────────┤
│{"name":"Paul Pogba"}│"windscreen" │1         │
├─────────────────────┼─────────────┼──────────┤
│{"name":"Paul Pogba"}│"door"       │1         │
└─────────────────────┴─────────────┴──────────┘

How to sum all values in column "count(i)" using Cypher in neo4j?

question from:https://stackoverflow.com/questions/65893421/how-to-sum-all-values-in-a-returned-column-cypher

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are forcing the split by returning c, i.kit, count(i). And what graph database does absolutely makes sense. To get aggregate just return count(i)

And if for some odd reason you seek to get the result you are asking.

MATCH (c:Customers {name:'Paul Pogba'})-[:ORDERED]->(o:Orders)-[:BOUGHT]->(i:Items)
WITH collect(DISTINCT c) AS _c, collect(DISTINCT i) AS _i, count(i) AS count_i
UNWIND _c AS c
UNWIND _i AS i
RETURN c, i.kit, count_i

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...