在同一查询中计算neo4j中节点的总数和小计

时间:2016-11-14 12:02:41

标签: neo4j cypher

我想在cypher中创建一个all in one语句来填充数据表。两个字段计算Sample的数量:一个显示总数,另一个显示没有额外:Ghost标签的数字:

 MATCH (a:Person)-[:OWNER]->(b:Project)-[:PROJECT]->(c:Import)-[:IMPORT|:INPUT|:OUTPUT*]->(d:Sample)
WITH a,b,c,d,d AS e WHERE NOT d:Ghost
RETURN DISTINCT b.Name,(a.`First Name` + " " + a.`Last Name`),b.Description,b.Date,count(DISTINCT c),count(DISTINCT d),count(DISTINCT e)

问题是de得出相同的值,而实际上e应该小于d。我怀疑de指向同一个地址。我可以通过MATCH再次查询图表来解决问题,但显然这是昂贵的,我宁愿只做一次。有可能吗?

1 个答案:

答案 0 :(得分:2)

WHERE会过滤WITH返回的内容,而您只会获得非Ghost个节点。改为使用CASE作为条件结果:

WITH a, b, c, d,
     CASE
         WHEN d:Ghost THEN null
         ELSE d
     END AS e

您还可以collect结果的不同样本节点,然后获取原始列表的大小和筛选列表(因此不会重复删除两次):

WITH DISTINCT b.Name AS projectName,
     (a.`First Name` + " " + a.`Last Name`) AS fullName,
     b.Description AS projectDescription,
     b.Date AS projectDate,
     count(DISTINCT c) AS importCount,
     collect(DISTINCT d) AS samples
RETURN projectName, fullName, projectDescription, projectDate, importCount,
       size(samples) AS sampleCount,
       size([s IN samples WHERE NOT s:Ghost]) as nonGhostCount