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
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.
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;
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;