简体   繁体   English

Neo4j Cypher 从单个查询返回多个计数

[英]Neo4j Cypher return multiple counts from a single query

For analytical purpose, I need to return multiple counts from a single query.出于分析目的,我需要从单个查询中返回多个计数。

For example, I have a User entity.例如,我有一个User实体。 User has active property true / false .用户具有active属性true / false

Is it possible with Cypher to write a single query which will return a total number of all users, and also 2 additional count for active and inactive users? Cypher 是否可以编写一个查询来返回所有用户的总数,以及活跃用户和非活跃用户的 2 个额外计数? If so, please show how.如果是这样,请说明如何。

Here is the counts of active and inactive users.这是活跃用户和非活跃用户的数量。 It is similar to SQL wherein it uses the sum() function and conditional clause "case when".它类似于 SQL,其中它使用 sum() function 和条件子句“case when”。

MATCH (n:Person) 
RETURN count(n) as user_counts, 
      sum(case when n.active then 1 end) as active, 
      sum(case when not n.active then 1 end) as inactive,
      sum(case when n.active is NULL then 1 end) as no_info

Sample result using Persons nodes in movie database使用电影数据库中的 Persons 节点的示例结果

╒═════════════╤════════╤══════════╤═════════╕
│"user_counts"│"active"│"inactive"│"no_info"│
╞═════════════╪════════╪══════════╪═════════╡
│133          │121     │7         │5        │
└─────────────┴────────┴──────────┴─────────┘

We can simply use:我们可以简单地使用:

Match(p:Persons)`
RETURN count(p) as total_user,
  sum(case when not p.active then 1 end) as inactive_users,
  sum(case when p.active then 1 end) as active_users,
  sum(case when p.active is NULL then 1 end) as remaining_users

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

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