2

我有一个像这样的密码查询。

START dep=node:cities(city_code = "JGS"), 
arr=node:cities(city_code = "XMN") 

MATCH dep-[way:BRANCH2BRANCH_AIRWAY*0..1]->()-->arr

RETURN length(way), transfer.city_code,
extract(w in way: w.min_consume_time) AS consumeTime

名为“way”的关系是可选的,因此当关系“way”不存在时,名为“consumeTime”的属性将是一个空列表。

查询结果为:

| 0 | “JGS” | [] |
| 1 | 《深圳》 | [3600] |

当我想使用属性“consumeTime”的头函数时,它返回错误“无效查询:空列表头”。

我怎样才能得到这样的结果?

| 0 | “JGS” | 空 |
| 1 | 《深圳》 | 3600 |

4

1 回答 1

1

这对于条件表达式来说是微不足道的,我认为添加到 Cypher 很重要:https ://github.com/neo4j/community/issues/899

reduce这是一个需要 1.9-SNAPSHOT 的用于您的有点 hacky 的查询:

START dep=node:cities(city_code = "JGS"), 
      arr=node:cities(city_code = "XMN") 
MATCH dep-[way:BRANCH2BRANCH_AIRWAY*0..1]->()-->arr
WITH  length(way) as wayLength, 
      transfer.city_code as transferCityCode,
      extract(w in way: w.min_consume_time) AS consumeTime
WITH  wayLength,
      transferCityCode,
      consumeTime,
      // reverse the consumeTime list, so that we can get the head from the end
      reduce(acc=[], x in consumeTime: x + acc) reverseConsumeTime
RETURN wayLength,
       transferCityCode,
       consumeTime,
       // return null if empty, else the end of the list
       reduce(acc=null, x in reverseConsumeTime: x) as headOrNull;

reverse(coll)可以使用以下语法来反转集合:或条件表达式以检查空列表,可以相当多地改进此查询 。

于 2012-10-23T16:39:22.193 回答