I need to do an aggregation on an aggregation in Cypher on Neo4j;
match (
match (w:words)
return distinct k.word as word, count(w) as count, count(distinct w.id) as id
) as a
return distinct id, count(word), sum(count);
Is this possible, google suggests not?
Try something like this using with:
match (w:words)
with distinct w.word as word, count(w) as count, count(distinct w.id) as id
return distinct id, count(word), sum(count);
Related
I have 2 subqueries that returns 2 sets of users (each query return one set of users)
First query :
MATCH (:User {user_id: "69b3315a-ba4a-4021-94e1-0f494f9b957f"})-->(first_set_of_users)
RETURN first_set_of_users
Second query :
MATCH (:User {user_id: "69b3315a-ba4a-4021-94e1-0f494f9b957f"})<-[:LIKES]-(likers)-[:LIKES]->(v)
WITH DISTINCT v
MATCH (second_set_of_users)-[:LIKES]->(v)
RETURN second_set_of_users, COUNT(*) AS recoWeight
ORDER BY recoWeight DESC
What I want to finally return is all users from second_set_of_users minus the one in first_set_of_users and ORDER BY recoWeight DESC
How can I do that in just one query ? Everything I tried led to cartesian products of queries and took forever while each independent query takes less than a second.
MATCH (:User {user_id: "69b3315a-ba4a-4021-94e1-0f494f9b957f"})-->(first_set_of_users)
WITH collect(first_set_of_users) AS list_of_first_set_of_users
MATCH (:User {user_id: "69b3315a-ba4a-4021-94e1-0f494f9b957f"})<-[:LIKES]-(likers)-[:LIKES]->(v)
WITH DISTINCT v, list_of_first_set_of_users
MATCH (second_set_of_users)-[:LIKES]->(v)
WITH second_set_of_users, COUNT(*) AS recoWeight
WHERE NOT second_set_of_users IN list_of_first_set_of_users
RETURN second_set_of_users, recoWeight
ORDER BY recoWeight DESC
Explanation.
Using WITH clause we could pass the result of the first query into the second query.
And then using WHERE NOT IN we could filter the result of the second query.
I am trying the following query,
start n=node(*) match (n)-[r]->(m) return count(r)
I am not sure if this query is fine.
You should use this query : MATCH ()-[r]->() RETURN count(*)
Cheers.
PS: The start, match query form should be only used for legacy index.
Try this query to count the number of relationships :
MATCH ()-[r:NAME_OF_RELATIONSHIP]->() RETURN count(r)
I have this query in SQL:
Select Id, CrawlerId,CrawlerName,
(SELECT Count(*) from CrawlerResult cr where cr.CrawlerId = cs.CrawlerId and IsNew=1) as LastRunResult ,
(SELECT Count(*) from CrawlerResult cr where cr.CrawlerId = cs.CrawlerId ) as TotalResult
FROM CrawlerScheduler cs
How to convert this query to neo4j cypher by combining CrawlerScheduler and CrawlerResult nodes?
I'm assuming you've replaced the foreign key relationships from SQL with actual relationships in Cypher, and that you're using actual booleans instead of 1 and 0? Something like:
(:CrawlerScheduler)-[:RESULT]->(:CrawlerResult)
If so then the equivalent Cypher query might look like this:
MATCH (cs:CrawlerScheduler)
WITH cs, SIZE((cs)-[:RESULT]->()) as TotalResult
OPTIONAL MATCH (cs)-[:RESULT]->(cr)
WHERE cr.IsNew
WITH cs, TotalResult, COUNT(cr) as LastRunResult
RETURN cs.Id, cs.CrawlerId, cs.CrawlerName, LastRunResult, TotalResult
EDIT
I changed the second match to an OPTIONAL MATCH, just in case the scheduler didn't have results, or didn't have new results.
MATCH (n)
RETURN DISTINCT id(n) as nid, n.name
ORDER BY n.name
SKIP 5
LIMIT 10
I'd like to get distinct nids and their name properties but instead, the query filters the whole row, i.e applies distinct keyword on "nid, n.name" as whole. How can I achieve to get distinct nids and names of the nodes which have those distinct nids?
I assume you're looking for the collect function:
MATCH (n)
RETURN id(n) as nid, collect(n.name)
SKIP 5
LIMIT 10
SQL has "Having" clause, for example:
SELECT LastName, COUNT(*)
FROM Employees
GROUP BY LastName
HAVING COUNT(*) > 10;
In Cypher, we can do count()
START n=node(2)
MATCH (n)-[r]->()
RETURN type(r), count(*)
But does Cypher have similar function as "Having", or is there any workaround?
Sure, having is just one of the many uses of query chaining with WITH which is similar to RETURN but determines which elements will be available in the next query part. WITH also supports ordering and paging.
START n=node(2)
MATCH (n)-[r]->()
WITH type(r) as t, count(*) as c
WHERE c > 10
RETURN t,c