0

so in neo4j, I have company nodes and relationships between them as invoices, relationship propertys as invoice amount and products description. I want to sum relationships amount to be left only one arrow, and sumerized amount, i don't want to delete relationships from db. i added this query to the bloom scene action but cant get any result

match(n:company)-[r:invoice]->(m:company) 
where id(n) in $nodes
with n, m, sum(r.amount) as total, collect(r) as relationships
where size(relationships) > 1
with total, head(relationships) as keep, tail(relationships) as delete
set keep.w = total
foreach(r in delete | delete r)
return *
jose_bacoy
  • 12,227
  • 1
  • 20
  • 38
ilia
  • 5
  • 2
  • I suspect this line is not getting data; id(n) in $nodes. Can you run the first match statement only and see if it returns data? match(n:company)-[r:invoice]->(m:company) where id(n) in $nodes – jose_bacoy Aug 15 '22 at 14:48
  • id(n) is neo4j assigned object id while n.id is the application id that you assigned. Both id(n) and n.id are different. – jose_bacoy Aug 15 '22 at 14:49
  • thank you for your answer, match(n:company)-[r:invoice]->(m:company) where id(n) in $nodes only this statment is working, – ilia Aug 15 '22 at 15:09
  • Good to know. So what is the error on the value of total? what is your expected value? – jose_bacoy Aug 15 '22 at 15:53
  • in scene action it says "This query manipulates data. All changes will be executed within the selected search timeout, although only the specified maximum number of unique nodes will be displayed. between my nodes, there are invoice relationship wich sometimes are one, or more then one, when there is one relationship, query is not working and it leaves original relationship, but if there is many relationships, when i run this query trough browser, it combines relationship, sums amount and leaves propertys of one relationship and deletes previous connections. – ilia Aug 15 '22 at 18:51
  • i dont know if there is a way to store Both, original and combined relationship to access as needed : ( – ilia Aug 15 '22 at 18:53

1 Answers1

0

You can use use virtual relationships that show the aggregated sums.

match(n:company)-[r:invoice]->(m:company) 
where id(n) in $nodes
with n,m, sum(r.amount) as amount, count(*) as rels
where rels > 1
return n,m,apoc.create.vRelationship(n,'TOTAL',{amount:amount},m) as rel
Michael Hunger
  • 41,339
  • 3
  • 57
  • 80