# Grouping values with Cypher query

**URL:** <https://community.neo4j.com/t/grouping-values-with-cypher-query/46184>\
**Category:** Graph Data Science / Graph Analytics\
**Tags:** cypher\
**Created:** [October 21, 2021, 7:46am UTC](https://community.neo4j.com/t/grouping-values-with-cypher-query/46184 "2021-10-21T07:46:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AjejeBrazorf](https://avatars.discourse-cdn.com/v4/letter/a/9f8e36/32.png) [@AjejeBrazorf](https://community.neo4j.com/u/AjejeBrazorf)\
**Post date:** [October 21, 2021, 7:46am UTC](https://community.neo4j.com/t/grouping-values-with-cypher-query/46184/1 "2021-10-21T07:46:38Z")

</div>

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 :

```auto
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

```auto
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:  
 ![query](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/6/8/6890f6f82fb1c31b8e06d7666f2c6d47064cbcbe.jpeg)  
 ![query1](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/1/4/149a654b9f6a68cab44875bbe562aa11fa30d4da.jpeg)

---

<div class="post-metadata">

**Author:** ![giuseppe\_villan](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/giuseppe_villan/32/11871_2.png) [@giuseppe\_villan](https://community.neo4j.com/u/giuseppe_villan)\
**Post date:** [October 21, 2021, 12:44pm UTC](https://community.neo4j.com/t/grouping-values-with-cypher-query/46184/2 "2021-10-21T12:44:44Z")

</div>

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

```auto
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`

---

<div class="post-metadata">

**Author:** ![AjejeBrazorf](https://avatars.discourse-cdn.com/v4/letter/a/9f8e36/32.png) [@AjejeBrazorf](https://community.neo4j.com/u/AjejeBrazorf)\
**Post date:** [October 21, 2021, 1:10pm UTC](https://community.neo4j.com/t/grouping-values-with-cypher-query/46184/3 "2021-10-21T13:10:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![giuseppe\_villan](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/giuseppe_villan/32/11871_2.png) [@giuseppe\_villan](https://community.neo4j.com/u/giuseppe_villan)\
**Post date:** [October 21, 2021, 3:15pm UTC](https://community.neo4j.com/t/grouping-values-with-cypher-query/46184/4 "2021-10-21T15:15:49Z")

</div>

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)

```auto
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

```

---

<div class="post-metadata">

**Author:** ![AjejeBrazorf](https://avatars.discourse-cdn.com/v4/letter/a/9f8e36/32.png) [@AjejeBrazorf](https://community.neo4j.com/u/AjejeBrazorf)\
**Post date:** [October 22, 2021, 7:29am UTC](https://community.neo4j.com/t/grouping-values-with-cypher-query/46184/5 "2021-10-22T07:29:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Bennu](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/bennu/32/11659_2.png) [@Bennu](https://community.neo4j.com/u/Bennu)\
**Post date:** [October 22, 2021, 3:58pm UTC](https://community.neo4j.com/t/grouping-values-with-cypher-query/46184/6 "2021-10-22T15:58:20Z")

</div>

Hi @AjejeBrazorf !

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

```auto
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

---

<div class="post-metadata">

**Author:** ![AjejeBrazorf](https://avatars.discourse-cdn.com/v4/letter/a/9f8e36/32.png) [@AjejeBrazorf](https://community.neo4j.com/u/AjejeBrazorf)\
**Post date:** [October 25, 2021, 8:27am UTC](https://community.neo4j.com/t/grouping-values-with-cypher-query/46184/7 "2021-10-25T08:27:09Z")

</div>

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

```auto

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
