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.

Grouping values with Cypher query

Hi. I posted some days ago a question about Cypher query on relationships. I repost the question here:
"Suppose to have the graph below.
I want to return all the nodes connected to the node A
and the maximum of the values on the edges between two consecutive nodes(which is an attribute of the relation). I show an example in the picture below."
The solution was :

MATCH path=(a {id:"A"})<-[:HAS*]-(b)
RETURN a,b, max([r in relationships(path) | r.value])

Now. Suppose that we have a second property/attribute on the relationship (which is called secondV in my example). How can I return also the second attribute in my query?
I tried

MATCH path=(a {id:"A"})<-[:HAS*]-(b)
RETURN a,b, max([r in relationships(path) | r.value]) , [r in relationships(path) | r.secondV]

But it returns to me all the possible couples of secondV with the maximum value I got.
The result should be as follow:
3X_6_8_6890f6f82fb1c31b8e06d7666f2c6d47064cbcbe.jpeg
3X_1_4_149a654b9f6a68cab44875bbe562aa11fa30d4da.jpeg

1 ACCEPTED SOLUTION

No. This doesn't work. I think I find the solution with this:


MATCH path=(startNode:Product {id: 'A'})-[:HAS*]->(endNode)
with  startNode,endNode, max([r in relationships(path) | value]) as valueList
match path = (startNode) - [:HAS*] - >(endNode)
with  startNode,endNode, valueList,[r in relationships(path) where value in valueList | secondV ] as secondValueList,path
where size(valueList) = size(secondValueList)
return distinct startNode,endNode,valueList,secondValueList, [x IN nodes(path) | x.id] as nodes 

I'm not sure if it works in every case

View solution in original post

6 REPLIES 6

@AjejeBrazorf
If I understand correctly, in this way it should work:

MATCH path=(a {id:"A"})<-[r:HAS]-(b)
with max(r.value) as max, a, b
call {with max, a, b match (a)<-[rel:HAS]-(b) where rel.value = max return rel limit 1}
return a, b, rel.value, rel.secondV

that is, I search for a max value,
then I call a subquery to retrieve the relation (for each a,b couple),
and finally I return value, secondV, a and b

This works for path of length one. I need to call the procedure for longer paths

Oh, I missed that, the drawing confused me :X

Then, can you try with this? (Basically the same logic as above, I added [0] to max in 2nd row because is a list)

MATCH path=(a {id:"A"})<-[:HAS*]-(b)
with max([r in relationships(path) | r.value])[0] as max, a, b
call {
with max, a, b match path=(a)<-[:HAS*]-(b) unwind relationships(path) as rel with rel, max where rel.value = max return rel limit 1
}
return a, b, rel.value, rel.secondV

I tried this solution, But it returns only the first value of the list:
value: 8 / secondV : 4
It doesn't return the other the relationships.

Bennu
Graph Fellow

Hi @AjejeBrazorf !

I'll suggest a solution with apoc for the sake of sanity

MATCH (a {id:"a"})
CALL apoc.path.expandConfig(a, {
	relationshipFilter: "HAS",
    minLevel: 1,
    uniqueness: 'NODE_PATH'
})
yield path 
with relationships(path) as rels, nodes(path) as no
unwind rels as r
RETURN head(no).id as begin, last(no).id as end, apoc.agg.maxItems(r, r.value).items

Bennu

No. This doesn't work. I think I find the solution with this:


MATCH path=(startNode:Product {id: 'A'})-[:HAS*]->(endNode)
with  startNode,endNode, max([r in relationships(path) | value]) as valueList
match path = (startNode) - [:HAS*] - >(endNode)
with  startNode,endNode, valueList,[r in relationships(path) where value in valueList | secondV ] as secondValueList,path
where size(valueList) = size(secondValueList)
return distinct startNode,endNode,valueList,secondValueList, [x IN nodes(path) | x.id] as nodes 

I'm not sure if it works in every case