Reputation: 2701
I want to limit by 50% of results from MATCH
but it looks like LIMIT
doesn't accept dynamic value.
I tried:
MATCH (:Profile)
WITH COUNT(*) AS c
MATCH (n:Profile)
WITH n ORDER BY rand() LIMIT toInt(c * 0.5)
RETURN n
Then I got the error:
It is not allowed to refer to variables in LIMIT
So is there any way to do that without using 2 separate queries?
Upvotes: 0
Views: 552
Reputation: 67019
If you have the apoc plugin installed, you can use the apoc.coll.randomItems function to get a random 50%:
MATCH (n:Profile)
WITH COLLECT(n) AS people
WITH apoc.coll.randomItems(people, SIZE(people)/2) as profile_lst
UNWIND profile_lst as prof
RETURN prof
Upvotes: 0
Reputation: 12704
This is how I see it.
MATCH (n:Profile)
WITH n, rand() as r ORDER by r
WITH collect(n) as profile_lst
WITH profile_lst, toInt(size(profile_lst)/2) as cnt
UNWIND profile_lst[0..cnt] as prof
RETURN prof
Upvotes: 2