We use cookies to improve your site experience and analyze website traffic. Click Accept to agree to our website's cookie use as described in our Cookie Policy . Click Preferences to customize your cookie settings.

Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results forΒ

- Neo4j
- Technical Discussions
- Neo4j Graph Platform
- Match statement based on unique relationship prope...

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Options

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

β09-07-2022 09:43 AM

Not sure if this is an unusual use case, but not able to find anything in the docs:

My graph has nodes labelled A and B, and relationships between those nodes with property C

I want to return just one A-B relationship per unique property C, for example if there are 4 relationships but only two unique values for C, the MATCH should only return two relationships

I have gotten the list of unique properties with a COLLECT DISTINCT, but I am stuck on the UNWIND.

Here is my current query:

```
MATCH ()-[x]-()
WITH COLLECT(DISTINCT x.c) AS props
UNWIND props as p
MATCH (a)-[z {c:p}]-(b) RETURN a,b,z LIMIT 1
```

The LIMIT 1 is returning only one relationship total, not one relationship per prop in p. Am I not understanding how UNWIND translates to a MATCH?

Many thanks,

Phil

Solved! Go to Solution.

1 ACCEPTED SOLUTION

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

β09-07-2022 10:40 AM - edited β09-07-2022 10:42 AM

I refactored your query to provide a working solution using your approach:

```
MATCH (:A)-[x]-(:B)
WITH DISTINCT x.c AS c
call {
WITH c
MATCH (a:A)-[r{c:c}]-(b:B)
RETURN a,b,r
LIMIT 1
}
RETURN a,b,r
```

The error in your original query is the 'LIMIT 1' is applied over the entire result, regardless of the value of 'c'. The use of a 'call' subquery, allows you to execute the query for each value of 'c' and apply the 'limit' clause agains just the results of that particular value of 'c'.

Note, this solution is executing the 'match' clause twice, compared to my earlier query. Once to get the distinct list of 'c' values, then again to get the relationships and limit each result to one row.

5 REPLIES 5

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

β09-07-2022 10:29 AM

Try something like this:

@phil_j_mcnamara wrote:

Not sure if this is an unusual use case, but not able to find anything in the docs:

My graph has nodes labelled A and B, and relationships between those nodes with property C

I want to return just one A-B relationship per unique property C, for example if there are 4 relationships but only two unique values for C, the MATCH should only return two relationships

I have gotten the list of unique properties with a COLLECT DISTINCT, but I am stuck on the UNWIND.

Here is my current query:

`MATCH ()-[x]-() WITH COLLECT(DISTINCT x.c) AS props UNWIND props as p MATCH (a)-[z {c:p}]-(b) RETURN a,b,z LIMIT 1`

The LIMIT 1 is returning only one relationship total, not one relationship per prop in p. Am I not understanding how UNWIND translates to a MATCH?

Many thanks,

Phil

```
match(:A)-[r]-(:B)
with r.c as c, collect(r)[0] as rel
return startNode(rel) as startNode, endNode(rel) as endNode, rel
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

β09-07-2022 10:40 AM - edited β09-07-2022 10:42 AM

I refactored your query to provide a working solution using your approach:

```
MATCH (:A)-[x]-(:B)
WITH DISTINCT x.c AS c
call {
WITH c
MATCH (a:A)-[r{c:c}]-(b:B)
RETURN a,b,r
LIMIT 1
}
RETURN a,b,r
```

The error in your original query is the 'LIMIT 1' is applied over the entire result, regardless of the value of 'c'. The use of a 'call' subquery, allows you to execute the query for each value of 'c' and apply the 'limit' clause agains just the results of that particular value of 'c'.

Note, this solution is executing the 'match' clause twice, compared to my earlier query. Once to get the distinct list of 'c' values, then again to get the relationships and limit each result to one row.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

β09-07-2022 12:58 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

β09-07-2022 02:58 PM

I think I understand. You would like to find one relationship for each unique value of 'c' between each pair of 'A' and 'B' nodes, instead of each unique value of 'c' regardless of the values of 'A' and 'B'.

Try this:

```
match(a:A)-[r]-(b:B)
where id(a) < id(b)
with a, b, r.c as c, collect(r)[0] as rel
return startNode(rel) as startNode, endNode(rel) as endNode, rel
```

OR

```
MATCH (a:A)-[x]-(b:B)
WHERE id(a) < id(b)
WITH a, b, collect(DISTINCT x.c) AS cList
UNWIND cList as c
call {
WITH a, b, c
MATCH (a)-[r{c:c}]-(b)
RETURN r
LIMIT 1
}
RETURN a,b,r
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

β09-07-2022 03:08 PM

Exactly, this works perfectly thank you so much!

Nodes 2022

OnΒ November 16 and 17 for 24 hours across all timezones, youβll learn about best practices for beginners and experts alike.

Related Content

- Link Prediction Pipeline for Protein Target Binding in Neo4j Graph Platform
- Neo4j LOAD CSV..when CSV columns contains a list of properties in Neo4j Graph Platform
- Getting relationship properties of shortest path in Neo4j Graph Platform
- Delete limit GraphQL in Neo4j Graph Platform
- Error in graphql in Drivers & Stacks