如何获得一个节点到另一个节点上的关系计数

时间:2019-11-13 10:17:42

标签: neo4j

这是我的问题的一个示例:

有一些节点和关系

  • (Amy:Staff)-[:出售{date:'2019-11-01'}]->(Car:Toy)
  • (Amy:Staff)-[:出售{date:'2019-11-02'}]->(Car:Toy)
  • (Amy:Staff)-[:出售{date:'2019-11-01'}]->(Plane:Toy)
  • (Amy:Staff)-[:出售{date:'2019-11-02'}]->(Plane:Toy)
  • (Amy:Staff)-[:出售{date:'2019-11-03'}]->(Plane:Toy)
  • (Amy:Staff)-[:出售{date:'2019-11-04'}]->(Plane:Toy)

那么我如何获得这样的表:

  • |员工|玩具|计数|
  • |艾米| [“汽车”,“飞机”] | [2,4] |

1 个答案:

答案 0 :(得分:0)

我认为这样的事情应该可以帮助您。

// find all the sale occurences
MATCH (staff:Staff)-[sale:SELL]-(toy:Toy)

// count the sales per staff and toy ordered by toy
WITH staff.name AS staff, toy.name AS toy, count(sale) AS sales
ORDER BY toy

// aggregate the toys and counts per staff member
RETURN staff, collect(toy) AS toys, collect(sales) AS total_sales