cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Create a Relationship with the total number of relations from one node to another

Hi all,
my graph is composed by a company entity and invoice relationships between companies. I'm able to create a summary relationship one by one.
My question is how I can do this for all the company in cypher?
At the moment I use Python to iterate all the companies
Here is the working query
MATCH (c:Company {companyId:24})-[r:INVOICE]->(t:Company {companyId:327})
WHERE r.invoiceState='PAID'
with c as azDa, t as azA, count(r) as numero, sum(r.amountGross) as dovuto
MERGE (azDa)-[s:summaryInvoice {numInvoice:numero,amountDue:dovuto}]->(azA)

Thank for your help

Regards
Rinaldo
1 ACCEPTED SOLUTION

:auto call {MATCH (c:Company)-[r:INVOICE]->(t:Company)
WHERE r.invoiceState='PAID'
with c, t, count(r) as numero,sum(r.amountGross) as dovuto
MERGE (c)-[s:summaryInvoice]->(t)
set s.numInvoice=numero, s.amountDue=dovuto
} in transactions of 1000 rows
this will create or update the summary relationship between every pair of company nodes with an invoice relationship. Is this what you want? You can add constraints if you want a subset instead. I enclosed it in a call subquery so the updates are done in batches. This is helpful if you have a large number of updates.

View solution in original post

2 REPLIES 2

:auto call {MATCH (c:Company)-[r:INVOICE]->(t:Company)
WHERE r.invoiceState='PAID'
with c, t, count(r) as numero,sum(r.amountGross) as dovuto
MERGE (c)-[s:summaryInvoice]->(t)
set s.numInvoice=numero, s.amountDue=dovuto
} in transactions of 1000 rows
this will create or update the summary relationship between every pair of company nodes with an invoice relationship. Is this what you want? You can add constraints if you want a subset instead. I enclosed it in a call subquery so the updates are done in batches. This is helpful if you have a large number of updates.

Hi Glilienfield,

thanks for suggestion, that's work fine.
Rinaldo