apoc.agg.multiStats
Function Apoc Extended
apoc.agg.multiStats(nodeOrRel, keys) - Return a multi-dimensional aggregation
Usage Examples
Given this dataset:
CREATE (:Person { louvain: 596, neo4jImportId: "18349390", wcc: 48, lpa: 598, name: "aaa", another: 548}),
(:Person { louvain: 596, neo4jImportId: "18349390", wcc: 48, lpa: 598, name: "eee", another: 549}),
(:Person { louvain: 596, neo4jImportId: "18349390", wcc: 48, lpa: 598, name: "eee", another: 549}),
(:Person { louvain: 597, neo4jImportId: "18349391", wcc: 48, lpa: 598, name: "eee", another: 549}),
(:Person { louvain: 597, neo4jImportId: "18349392", wcc: 47, lpa: 596, name: "iii", another: 549}),
(:Person { louvain: 597, neo4jImportId: "18349393", wcc: 47, lpa: 596, name: "iii", another: 549}),
(:Person { louvain: 597, neo4jImportId: "18349394", wcc: 47, lpa: 596, name: "iii", another: 549}),
(:Person { louvain: 597, neo4jImportId: "18349393", wcc: 47, lpa: 596, name: "iii", another: 10}),
(:Person { louvain: 597, neo4jImportId: "18349394", wcc: 47, lpa: 596, name: "iii", another: 10})
We can create an optimized multiple aggregation based on the property key, similar to this one:
MATCH (p:Person)
WITH p
CALL {
WITH p
MATCH (n:Person {louvain: p.louvain})
RETURN sum(p.louvain) AS sumLouvain, avg(p.louvain) AS avgLouvain, count(p.louvain) AS countLouvain
}
CALL {
WITH p
MATCH (n:Person {wcc: p.wcc})
RETURN sum(p.wcc) AS sumWcc, avg(p.wcc) AS avgWcc, count(p.wcc) AS countWcc
}
CALL {
WITH p
MATCH (n:Person {another: p.another})
RETURN sum(p.another) AS sumAnother, avg(p.another) AS avgAnother, count(p.another) AS countAnother
}
CALL {
WITH p
MATCH (lpa:Person {lpa: p.lpa})
RETURN sum(p.lpa) AS sumLpa, avg(p.lpa) AS avgLpa, count(p.lpa) AS countLpa
}
RETURN p.name,
sumLouvain, avgLouvain, countLouvain,
sumWcc, avgWcc, countWcc,
sumAnother, avgAnother, countAnother,
sumLpa, avgLpa, countLpa
executing the following query:
MATCH (p:Person)
RETURN apoc.agg.multiStats(p, ["lpa","wcc","louvain", "another"]) as output
output |
---|
|
which can be used, for example, to return a result similar to the Cypher one in this way:
MATCH (p:Person)
WITH apoc.agg.multiStats(p, ["lpa","wcc","louvain", "another"]) as data
MATCH (p:Person)
RETURN p.name,
data.wcc[toString(p.wcc)].avg AS avgWcc,
data.louvain[toString(p.louvain)].avg AS avgLouvain,
data.lpa[toString(p.lpa)].avg AS avgLpa
avgWcc | avgLouvain | avgLpa |
---|---|---|
48.0 |
596.0 |
598.0 |
48.0 |
596.0 |
598.0 |
48.0 |
596.0 |
598.0 |
47.0 |
597.0 |
596.0 |
47.0 |
597.0 |
596.0 |
47.0 |
597.0 |
596.0 |
47.0 |
597.0 |
596.0 |
47.0 |
597.0 |
596.0 |