Text 如何在Neo4j中将选择性查询结果连接为字符串? 初始情况

Text 如何在Neo4j中将选择性查询结果连接为字符串? 初始情况,text,neo4j,cypher,Text,Neo4j,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;

我编写了一个有效的密码查询,它返回四个不同的数量

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;
(因为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;
仍然开放:

  • 由于值0,正在筛选amountB
您想使用该功能吗

匹配
具有
将(不同类型A)计数为amountA,
将(不同类型B)计数为数量B,
将(不同类型C)计数为数量C,
将(不同类型的)计数为amountD
//重新格式化为列表
具有
[{name:'amountA',value:amountA},{name:'amountB',value:amountB},{name:'amountC',value:amountC},{name:'amountD',value:amountD}]作为数量
//过滤掉0
使用过滤器(数量中的x,其中x.value>0)作为数量
//将列表转换为字符串
回来
减少(结果=数量[0]。名称+“:”+数量[0]。值,尾部的n(数量)|结果+“,“+n.名称+”:“+n.值)作为格式化数量;作为格式化的数量;
请注意,如果所有值都为0(null+string=null),则返回null

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"│
└─────────────────────────────────────────────────────────────────┘
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;