nad.SN
nad.SN

Reputation: 77

neo4j sum up values

I'm trying to get Total(Sum) of a property of "df" object.I have attached screen shot of sample database I'm using

I tried to get the graph using following query

   MATCH P= (n:Org)-[:O_CH*0..]->()-[:LEAF*0..]->()-[:CH*0..]->()-[:FOR*0..]->() RETURN P

To create objects

create(n:Org{name:'Root',id:1})

create(n:Org{name:'L1.1',id:2,parent:1})
create(n:Org{name:'L1.2',id:3,parent:1})
create(n:Org{name:'L1.3',id:4,parent:1})

create(n:Org{name:'L2.1',id:5,parent:3})
create(n:Org{name:'L2.2',id:6,parent:4})

create(n:Op{name:'CH1',id:7,orgId:5})
create(n:Op{name:'CH2',id:8,orgId:5 ,parent:'CH1'})

create(n:Proj{name:'P1',id:9,opp:'CH2'})
create(n:Proj{name:'P2',id:10,opp:'CH1'})

create(n:R{name:'R1',id:200,orgId:2 })

create (n:df{id:100,map:8,forecast:toFloat(10)})
create (n:df{id:101,map:7,forecast:toFloat(10)})
create (n:df{id:102,map:9,forecast:toFloat(10)})
create (n:df{id:103,map:10,forecast:toFloat(10)})
create (n:df{id:104,map:200,forecast:toFloat(10)})

To Crate relationships

    MATCH (c:Org),(p:Org) WHERE c.parent = p.id create (p)-[:O_CH]->(c)   
    MATCH (c:Op),(p:Op) WHERE c.parent = p.name create (p)-[:CH]->(c)    
    MATCH (c:Op),(p:Org) WHERE c.orgId = p.id create (p)-[:LEAF]->(c)  
    MATCH (c:Proj),(p:Op) WHERE c.opp = p.name create (p)-[:CH]->(c)      
    MATCH (c:R),(p:Org)  WHERE c.orgId = p.id  create (p)-[:LEAF]->(c)     
    MATCH (c:df),(p:) WHERE c.map = p.id  create (p)-[:FOR]->(c)

I'm expecting 60 as total where I get 260 as total. Please let me know where I'm wrong . Need your help to figure out.

Upvotes: 2

Views: 792

Answers (1)

Bruno Peres
Bruno Peres

Reputation: 16365

I'm trying to get Total(Sum) of a property of "df" object.

I believe you needs a simple query that match all nodes with label :df and return the sum of node.forecast. Try it:

// Match all nodes with :df label
MATCH(n:df)
// Return the sum of the property 'forecast' of each matched node
RETURN sum(n.forecast)

From comments:

Thank you Bruno.But the thing i need to get the aggregated value as a example if i select L2.1 , i need to get the sum of df objects which are under that node

This should work:

MATCH({name:'L2.1'})-[*]->(n)
RETURN SUM(n.forecast)

Upvotes: 1

Related Questions