Hi,
I’m getting unexpected results when using count() in case when clause in conjunction with exists().
Example:
Create 1 Author and 4 Book nodes.
Create 4 WROTE relations with year property. One of the relations does not have year.
graph.query test "CREATE
(kavish:Author {name: 'Kavish'}),
(book1:Book {name: 'book1'}),
(book2:Book {name: 'book2'}),
(book3:Book {name: 'book3'}),
(book4:Book {name: 'book4'}),
(kavish)-[:WROTE{year:2001}]->(book1),
(kavish)-[:WROTE{year:2002}]->(book2),
(kavish)-[:WROTE{year:2003}]->(book3),
(kavish)-[:WROTE]->(book4)"
Count of all relations
graph.query test "match (a:Author{name:'Kavish' })-[r:WROTE]->(n) return count(r) as cnt"
this returns 4 as expected.
However when I try to count the relations which have year property set with query:
graph.query test "match (a:Author{name:'Kavish' })-[r:WROTE]->(n) return case when exists(r.year) then count(r) else 0 end as cnt"
this is returning 0 as opposed to 3.
1) 1) "cnt"
2) 1) 1) "0"
The following query returns 3 true and 1 false values as expected
graph.query test "match (a:Author{name:'Kavish'})-[r:WROTE]->(n) return exists(r.year)"
1) 1) "exists(r.year)"
2) 1) 1) "true"
2) 1) "true"
3) 1) "true"
4) 1) "false"
but somehow the count() is not returning correct value.
I got it working using sum() instead of count() as:
graph.query test "match (a:Author{name:'Kavish' })-[r:WROTE]->(n) return sum(case when exists(r.year) then 1 else 0 end) as cnt"
1) 1) "cnt"
2) 1) 1) "3"
however this won’t work when I want to get distinct counts.
Could someone please help me on this? Or point out if I’m missing anything.
Thanks in advance