count函数

count()函数可以计数指定的值或行数。

  • (nGQL扩展)用户可以同时使用count()GROUP BY对指定的值进行分组和计数,再使用YIELD返回结果。

  • (openCypher方式)用户可以使用count()对指定的值进行计数,再使用RETURN返回结果。不需要使用GROUP BY

语法

  1. count({expr | *})
  • count(*)返回总行数(包括NULL)。

  • count(expr)返回满足表达式的非空值的总数。

  • count()size()是不同的。

示例

  1. nebula> WITH [NULL, 1, 1, 2, 2] As a UNWIND a AS b \
  2. RETURN count(b), count(*), count(DISTINCT b);
  3. +----------+----------+-------------------+
  4. | COUNT(b) | COUNT(*) | COUNT(distinct b) |
  5. +----------+----------+-------------------+
  6. | 4 | 5 | 2 |
  7. +----------+----------+-------------------+
  1. # 返回player101 follow的人,以及follow player101的人,即双向查询。
  2. nebula> GO FROM "player101" OVER follow BIDIRECT \
  3. YIELD $$.player.name AS Name \
  4. | GROUP BY $-.Name YIELD $-.Name, count(*);
  5. +---------------------+----------+
  6. | $-.Name | COUNT(*) |
  7. +---------------------+----------+
  8. | "Dejounte Murray" | 1 |
  9. +---------------------+----------+
  10. | "LaMarcus Aldridge" | 2 |
  11. +---------------------+----------+
  12. | "Tim Duncan" | 2 |
  13. +---------------------+----------+
  14. | "Marco Belinelli" | 1 |
  15. +---------------------+----------+
  16. | "Manu Ginobili" | 1 |
  17. +---------------------+----------+
  18. | "Boris Diaw" | 1 |
  19. +---------------------+----------+

上述示例的返回结果有两列:

  • $-.Name:查询结果包含的姓名。

  • COUNT(*):姓名出现的次数。

因为测试数据库basketballplayer中没有重复的姓名,COUNT(*)列中数字2表示该行的人和player101是互相follow的关系。

  1. # 方法一:统计数据库中的年龄分布情况。
  2. nebula> LOOKUP ON player \
  3. YIELD player.age As playerage \
  4. | GROUP BY $-.playerage \
  5. YIELD $-.playerage as age, count(*) AS number \
  6. | ORDER BY number DESC, age DESC;
  7. +-----+--------+
  8. | age | number |
  9. +-----+--------+
  10. | 34 | 4 |
  11. +-----+--------+
  12. | 33 | 4 |
  13. +-----+--------+
  14. | 30 | 4 |
  15. +-----+--------+
  16. | 29 | 4 |
  17. +-----+--------+
  18. | 38 | 3 |
  19. +-----+--------+
  20. ...
  21. # 方法二:统计数据库中的年龄分布情况。
  22. nebula> MATCH (n:player) \
  23. RETURN n.age as age, count(*) as number \
  24. ORDER BY number DESC, age DESC;
  25. +-----+--------+
  26. | age | number |
  27. +-----+--------+
  28. | 34 | 4 |
  29. +-----+--------+
  30. | 33 | 4 |
  31. +-----+--------+
  32. | 30 | 4 |
  33. +-----+--------+
  34. | 29 | 4 |
  35. +-----+--------+
  36. | 38 | 3 |
  37. +-----+--------+
  38. ...
  1. # 统计Tim Duncan关联的边数。
  2. nebula> MATCH (v:player{name:"Tim Duncan"}) -- (v2) \
  3. RETURN count(DISTINCT v2);
  4. +--------------------+
  5. | COUNT(distinct v2) |
  6. +--------------------+
  7. | 11 |
  8. +--------------------+
  9. # 多跳查询,统计Tim Duncan关联的边数,返回两列(不去重和去重)。
  10. nebula> MATCH (n:player {name : "Tim Duncan"})-[]->(friend:player)-[]->(fof:player) \
  11. RETURN count(fof), count(DISTINCT fof);
  12. +------------+---------------------+
  13. | COUNT(fof) | COUNT(distinct fof) |
  14. +------------+---------------------+
  15. | 4 | 3 |
  16. +------------+---------------------+