Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do a WHERE clause on a COUNT in Cypher?

Tags:

cypher

This is what I'm trying:

match 
(e:Person) - [r] - (f:Person) 
where (count(r) > 5 AND count (r) <10) 
return id(e), e.name; 

I get

QueryExecutionKernelException: Invalid use of aggregating function count(...) in this context

Basically I'm wanting to find a Person who is related to between 5 and 10 other Persons.

like image 584
dwjohnston Avatar asked Jul 09 '15 23:07

dwjohnston


People also ask

How do you use case in Cypher?

We can use CASE to evaluate a boolean condition and output a single-element list, or an empty list, and this drives the conditional Cypher execution (to execute the subsequent write-only clauses, or not).

What happens if list is empty in Cypher?

If the list is empty, then the contained Cypher will not execute. We can use CASE to evaluate a boolean condition and output a single-element list, or an empty list, and this drives the conditional Cypher execution (to execute the subsequent write-only clauses, or not).

How do I execute a cypher in a single element list?

If a list has 1 element, then the Cypher in the FOREACH will execute. If the list is empty, then the contained Cypher will not execute. We can use CASE to evaluate a boolean condition and output a single-element list, or an empty list, and this drives the conditional Cypher execution (to execute the subsequent write-only clauses, or not).

How to use the HAVING clause in a where clause?

COUNT, MAX, etc.) in A WHERE clause. Hence we use the HAVING clause instead. Therefore the whole query would be similar to this: SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name HAVING aggregate_function(column_name) operator value;


1 Answers

to find out people who are connected with each other by more than relationship ex:

  • a likes b
  • a knows b
  • a lives_with b

use

match (a:Person)-[r]-(b:person)
with a,b,count(r) as cnt
where cnt > 5 and cnt < 10
return *

if however you want to find people who are connected as a chain ( friends of friends )

  • a knows b
  • b knows c
  • c likes d
  • d knows e

and you want to find a to d then you can use something like

MATCH (n:Person)-[r*1..3]->(m:Person)
RETURN *

relevant tutorial heer

like image 173
cechode Avatar answered Sep 20 '22 17:09

cechode