我编写了一个有效的Cypher查询,该查询返回了四个不同的数量。
MATCH
<complex satement>
WITH
count(DISTINCT typeA) AS amountA,
count(DISTINCT typeB) AS amountB,
count(DISTINCT typeC) AS amountC,
count(DISTINCT typeD) AS amountD
RETURN
amountA, amountB, amountC, amountD;
我现在想返回一个文本字符串,而不是四列表,其中所有四个量都串联在一起,包括描述性标签。但是,如果数量大于零,则数量只能是字符串的一部分。
╒════════════════════════════════════════════════════╕
│"formattedQuantities" │
╞════════════════════════════════════════════════════╡
│"amountA: 123456, amountC: 9876543, amountD: 2018" │
└────────────────────────────────────────────────────┘
(由于amountB的值为0,因此在结果中将其省略。)
我将此Cyper查询用于数百万行。由于担心性能影响,我不想创建和调用自定义插件。
那么,如何使用Cypher和Neo4j以字符串形式返回数量?您能给我建议如何解决这个挑战吗?在此先感谢您为我指明了正确的方向!
密码声明:
MATCH
<complex satement>
WITH
count(DISTINCT typeA) AS amountA,
count(DISTINCT typeB) AS amountB,
count(DISTINCT typeC) AS amountC,
count(DISTINCT typeD) AS amountD
WITH
['amountA: ', amountA, ', amountB: ', amountB, ', amountC: ', amountC, ', amountD: ', amountD] AS quantities
RETURN
reduce(result = toString(head(quantities)), n IN tail(quantities) | result + n) AS formattedQuantities;
结果:
╒═════════════════════════════════════════════════════════════════╕
│"formattedQuantities" │
╞═════════════════════════════════════════════════════════════════╡
│"amountA: 123456: 1, amountB: 0, amountC: 9876543, amountD: 2018"│
└─────────────────────────────────────────────────────────────────┘
仍然打开:
答案 0 :(得分:1)
您要使用FILTER函数
MATCH
<complex satement>
WITH
count(DISTINCT typeA) AS amountA,
count(DISTINCT typeB) AS amountB,
count(DISTINCT typeC) AS amountC,
count(DISTINCT typeD) AS amountD
// Reformat to list
WITH
[{name:'amountA', value:amountA}, {name:'amountB', value:amountB}, {name:'amountC', value:amountC}, {name:'amountD', value:amountD}] AS quantities
// Filter out 0's
WITH filter(x IN quantities WHERE x.value > 0) AS quantities
// Convert list to string
RETURN
reduce(result = quantities[0].name + ": " + quantities[0].value, n IN tail(quantities) | result + ", " + n.name + ": " + n.value) AS formattedQuantities; AS formattedQuantities;
请注意,如果所有值均为0(null +字符串= null),则返回null