Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...