Jovo Krneta
Jovo Krneta

Reputation: 558

Neo4J find unique results

I have a graph that displays a finance relation between companies - the relation of owes , which companies owe money to companies . I seek a unique relations - circles , which are latter closed .So if I owe You money and we find that somehow you owe me money me close the debt . The companies are identified by tax number. For this I use this Cypher query :

start n=node(*) 
match p=n-[r:OWES*1..200]->n 
where HAS(n.taxnumber) 
return  extract(s in relationships(p) : s.amount), 
        extract(t in nodes(p) : ID(t)), 
        length(p) ;

But I also get results like

Company1-Company2-company1-Company-3

I display this results back in my java application . Should I maybe hide this results after I parse them in java code - results where one company is shown twice . This is fine when it comes to logic but I need results where a company is shown only once , I do not want results where I get the same company multiple times . How to modify my Cypher query for that ? What I want is that the company in the results can be only at the beginning and at the end of the result and not somehow circled in the middle .

Upvotes: 0

Views: 106

Answers (1)

Michael Hunger
Michael Hunger

Reputation: 41676

You can try to check the path-nodes to not contain your start node.

start n=node(*) 
match p=n-[:OWES*1..200]->(m), (m)-[r:OWES]->n 
where HAS(n.taxnumber) 

AND NOT(n IN tail(nodes(p)))

return  extract(s in relationships(p) : s.amount) + r.amount, 
        extract(t in nodes(p) : ID(t)) + ID(n), 
        length(p) + 1;

Unfortunately there is no subscript in 1.8.2 and only tail(coll) no simple way to exclude the last element from a check. That's why I have to break up p and fix your aggregations at the end.

Upvotes: 2

Related Questions