Cypher Count返回null而不是0

时间:2013-11-20 21:13:16

标签: neo4j cypher

如何以零计数进行此返回?任何时候我想要的任何字段返回等于零,整个查询返回null?我一直遇到这个问题,我迫切需要一个解决方案。

START bp=node({startnodeid}) 
WITH bp 
MATCH (bp)<-[r1?:VIEWED_PROFILE]-() 
WHERE r1.timestamp > {weekStart} AND r1.timestamp < {firstDay} 
WITH count(r1) AS dayOne, bp 
MATCH (bp)<-[r2?:VIEWED_PROFILE]-() 
WHERE r2.timestamp > {firstDay} AND r2.timestamp < {secondDay}
WITH count(r2) AS dayTwo, dayOne, bp 
MATCH (bp)<-[r3?:VIEWED_PROFILE]-() 
WHERE r3.timestamp > {secondDay} AND r3.timestamp < {thirdDay} 
WITH count(r3) AS dayThree, dayTwo, dayOne, bp 
MATCH (bp)<-[r4?:VIEWED_PROFILE]-() 
WHERE r4.timestamp > {thirdDay} AND r4.timestamp < {fourthDay}
WITH count(r4) AS dayFour, dayThree, dayTwo, dayOne, bp 
MATCH (bp)<-[r5?:VIEWED_PROFILE]-() 
WHERE r5.timestamp > {fourthDay} AND r5.timestamp < {fifthDay}
WITH count(r5) AS dayFive, dayFour, dayThree, dayTwo, dayOne, bp 
MATCH (bp)<-[r6?:VIEWED_PROFILE]-() 
WHERE r6.timestamp > {fifthDay} AND r6.timestamp < {sixthDay}
WITH count(r6) AS daySix, dayFive, dayFour, dayThree, dayTwo, dayOne, bp 
MATCH (bp)<-[r7?:VIEWED_PROFILE]-() 
WHERE r7.timestamp > {sixthDay} AND r7.timestamp < {seventhDay}
RETURN count(r7) AS daySeven, daySix, dayFive, dayFour, dayThree, dayTwo, dayOne

出于统计原因,我需要前七天的个人资料视图。

Neo console:http://console.neo4j.org/?id=cy5v5k

在此控制台示例中,时间戳是奇数,其中我的参数如下:

weekStart = 0;
firstDay = 2;
secondDay = 4;
thirdDay = 6;
fourthDay = 8;
fifthDay = 10;
sixthDay = 12;
seventhDay = 14;

我的预期回报值如下:

dayOne = 1;
dayTwo = 3;
dayThree = 2;
dayFour = 1;
dayFive = 1;
daySix = 4;
daySeven = 0;

1 个答案:

答案 0 :(得分:4)

您使用的是2.0吗?如果是这样,您可能应该完全重新组织此查询以使用CASE/WHEN并在那里打破您一周的日期,而不是单独执行WITH子句。 http://console.neo4j.org/r/utsrzy

MATCH (n)-[r:VIEWED_PROFILE]->(p) 
RETURN sum(CASE WHEN r.timestamp > 0 AND r.timestamp < 2 THEN 1 ELSE 0 END) AS day1,
       sum(CASE WHEN r.timestamp > 2 AND r.timestamp < 4 THEN 1 ELSE 0 END) AS day2, 
       sum(CASE WHEN r.timestamp > 4 AND r.timestamp < 6 THEN 1 ELSE 0 END) AS day3, 
       sum(CASE WHEN r.timestamp > 6 AND r.timestamp < 8 THEN 1 ELSE 0 END) AS day4, 
       sum(CASE WHEN r.timestamp > 8 AND r.timestamp < 10 THEN 1 ELSE 0 END) AS day5,
       sum(CASE WHEN r.timestamp > 10 AND r.timestamp < 12 THEN 1 ELSE 0 END) AS day6, 
       sum(CASE WHEN r.timestamp > 12 AND r.timestamp < 14 THEN 1 ELSE 0 END) AS day7

如果您使用的是1.9而不是count(r7)等,请执行count(1)

更新,快速修复不适用于1.9。这里有一些收集操作,以获得1.9为您执行此操作:

http://console.neo4j.org/r/cata9u

START bp=node(1) 
MATCH (bp)<-[r1?:VIEWED_PROFILE]-() 
WITH collect(r1.timestamp) AS times 
RETURN length(filter(t IN times WHERE t > 0 AND t < 2)) AS day1, 
       length(filter(t IN times WHERE t > 2 AND t < 4)) AS day2, 
       length(filter(t IN times WHERE t > 12 AND t < 14)) AS day7