简体   繁体   English

Cypher Neo4j ORDER BY DESC查询

[英]Cypher Neo4j ORDER BY DESC query

I want to order the COUNT(Movie.title) in descending order. 我想按降序排列COUNT(Movie.title)。 But it gives an error. 但它给出了一个错误。 This is the query. 这是查询。

MATCH (Movie {genre:"Action"})<-[:ACTS_IN]-(Person)
                 "RETURN Person.name, Movie.genre,  COUNT(Movie.title)"
                 "ORDER BY COUNT(Movie.title) DESC"
                 "LIMIT 100";

Thanks! 谢谢!

You can use this query: 您可以使用此查询:

MATCH (movie:Movie {genre:"Action"})<-[:ACTS_IN]-(person:Person)
RETURN person.name, movie.genre,  COUNT(distinct movie.title) AS cnt
ORDER BY cnt DESC
LIMIT 100

The error is returned because you cannot order by an aggregate immediately in Cypher. 返回错误是因为您无法立即在Cypher中按聚合顺序排序。 To order by any aggregate you need to use the WITH operator. 要按任何聚合订购,您需要使用WITH运算符。

So your query should be (assumes that you want to list the titles per actor per genre): 所以你的查询应该是(假设你想列出每个类型的每个演员的标题):

MATCH (Movie {genre:"Action"})<-[:ACTS_IN]-(Person)
RETURN Person.name, Movie.genre,  COUNT(Movie.title)
WITH Person.name AS name, Movie.genre AS genre, COLLECT(Movie.title) AS titles
RETURN name, genre, titles
ORDER BY LENGTH(titles) DESC
LIMIT 100

The limit 100 has now changed its behaviour so you probably want to move it up into the query: 限制100现在已经改变了它的行为,因此您可能希望将其移动到查询中:

MATCH (Movie {genre:"Action"})<-[:ACTS_IN]-(Person)
RETURN Person.name, Movie.genre,  COUNT(Movie.title)
WITH Person, Movie
LIMIT 100
WITH Person.name AS name, Movie.genre AS genre, COLLECT(Movie.title) AS titles
RETURN name, genre, titles
ORDER BY LENGTH(titles) DESC

Aside: to make your queries perform well you should have an Index on the Movie.genre property and you should introduce labels for Movie and Person. 除此之外:为了使您的查询运行良好,您应该在Movie.genre属性上有一个索引,您应该为Movie和Person引入标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM