Limit to single Relationship when using UNWIND

Hi all, I'm having problems when trying to only return 1 node when using UNWIND.

UNWIND [ 1, 2, 3 ] as id 
    OPTIONAL MATCH (client:Client)-[:CLIENT_FOR]->(l:Letter) WHERE l.id = id
    RETURN client.name

In some cases, a letter may have more than one client, but I always want to limit the result to exactly 3 clients (One client for each letter). It doesn't matter which client I get for the letter, but with the above statement, I may get more than 3 client names.

I tried adding LIMIT 1 after the RETURN but that limits the result to one client.

Try this:

UNWIND [ 1, 2, 3 ] as id 
match (c:Client)-[:CLIENT_FOR]-(a:Letter) where a.id = id
with c
with collect(distinct c.name) as c1
match (b:Client) where b.name in apoc.coll.toSet(c1)
return b.name

Thanks for the repsonse.. Unfortunately, that generated a Neo.DatabaseError.Statement.ExecutionFailed!

[Neo.DatabaseError.Statement.ExecutionFailed]: null, reference e73288e4-a00a-4d1d-ae32-b5558a452cba.org.neo4j.fabric.executor.FabricException

I opened a support ticket and hopefully then can find out what happened.

I am running Enterprise 4.1.3

UPDATE - I added CYPHER runtime=slotted and the statement works! Well kind of... the thing is I dont want Distinct Names.. they can be repeated ;-)

I'm afraid I'm something obvious.... Why not use LIMIT 3?

Also, I'm not sure why you are using OPTIONAL because I think that could include clients for where there is no letter.

If you're worried that the count is less than 3, you could put in a conditional that tests the count to see if it's less than 3 and output something useful.

Hi there.. the LIMIT param just truncates the list.

For example if each letter has 2 clients (total 6 clients) the LIMIT 3 will probably select 2 clients from letter 1 and 1 client from letter 2.

I want one client for each letter even if they all have the same client.

Good evening,
I think you can do something with only one relation per letter.
(c:Client)-[r:CLIENT_FOR]->(l:Letter) the "r" has a ID you can read it by ID(r).
I'am note sure if i understand your case enough.

But this is what I came up with:
"I want one client for each letter even if they all have the same client."
First get only the a single relation per letter:

 MATCH (:Client)-[r:CLIENT_FOR]->(l:Letter)  WITH l, collect(id(r))[0] AS rid 

And then get "a" Client from de relation ID:

MATCH (c:Client)-[r:CLIENT_FOR]->(l:Letter) WHERE id(r) = rid RETURN c.name;

Yours kindly Omer

1 Like

Thanks Omer that worked! Here is the final result

UNWIND [ 1, 2, 3 ] as id 
MATCH (:Client)-[r:CLIENT_FOR]->(l:Letter) WHERE l.uuid = id
WITH l, COLLECT(id(r))[0] AS rid 
MATCH (c:Client)-[r:CLIENT_FOR]->(l:Letter) WHERE id(r) = rid 
RETURN c.name;