user1325194
user1325194

Reputation: 175

Cypher Count is returning null instead of 0

How do I make this return with zero counts? Any time ANY of the fields I want returned equals zero, the whole query returns null? I keep running into this problem and I desperately need a solution.

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

I need the profile views for the previous seven days for statistical reasons.

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

In this console example, the timestamps are odd numbers where my parameters were as follows:

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

and my expected return values were as follows:

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

Upvotes: 0

Views: 1203

Answers (1)

Eve Freeman
Eve Freeman

Reputation: 33175

Are you using 2.0? If so, you should probably be reorganizing this query entirely to use CASE/WHEN and breaking out your days of the week there, instead of doing separate WITH clauses. 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

If you're using 1.9, instead of count(r7), etc., do count(1).

Update, that quick fix didn't work for 1.9. Here's a bit of collection manipulation to get 1.9 to do this for you:

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

Upvotes: 5

Related Questions