我想在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)
问题是d
和e
得出相同的值,而实际上e
应该小于d
。我怀疑d
和e
指向同一个地址。我可以通过MATCH
再次查询图表来解决问题,但显然这是昂贵的,我宁愿只做一次。有可能吗?
答案 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