SET in combination with CASE statement in cypher

假如想象 提交于 2019-12-04 07:11:17

This did it! Also see console at http://console.neo4j.org/?id=rq2i7j

MATCH (u:user)-[int:INTEREST]->(t:term)<-[:ISABOUT]-(d:doc)<-[r:RELEVANCE]-(u)
WITH int, t, 
     SUM(CASE WHEN r.value= 1 THEN 1 ELSE 0 END ) AS poscnt, 
     SUM(CASE WHEN r.value= -1 THEN 1 ELSE 0 END ) AS negcnt
SET int.pos=poscnt,int.neg=negcnt
RETURN t.name,int.pos,int.neg

Is it important for you to keep positive and negative count separate? It seems you could have a score property summing positive and negative values.

MATCH (u:user)-[int:INTEREST]->()<-[:ISABOUT]-()<-[r:RELEVANCE]-(u)
SET int.score = SUM(r.value)
RETURN t.name, int.score

You already seem to have found a working solution but I'll add a note about CASE as I understand it. While CASE provides branching, I think it's correct to say that it is an expression and not a statement. It resembles a ternary operator more than a conditional statement. As the expression

a > b ? x : y;

is resolved to a value, either x or y, that can be used in a statement, so also

CASE WHEN a > b THEN x ELSE y END

resolves to a value. You can then assign this value

result = CASE WHEN a > b THEN x ELSE y END

Your original query used the CASE expression like a conditional statement

CASE WHEN a > b THEN result = x ELSE result = y END

which resembles if-else

if a > b { result = x; } else { result = y; }

Someone may want to correct the terminology, the point is that in your working query you correctly let CASE resolve to a value to be used by SUM rather than put a conditional assignment inside CASE.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!