質問

I have a query

start ko=node:koid('ko:"ko:K01963"') 
match p=(ko)-[r:abundance*1]-(n:Taxon) 
with p, extract(nn IN relationships(p) | nn.`c1.mean`) as extracted 
return extracted;

I would like to sum the values in extracted by using return sum(extracted), however, this throws me the following error

SyntaxException: Type mismatch: extracted already defined with conflicting type Collection<Boolean>, Collection<Number>, Collection<String> or Collection<Collection<Any>> (expected Relationship)`

Also, when i return extracted, my values are enclosed in square brackets

+---------------+
| extracted     |
+---------------+
| [258.98813]   |
| [0.0]         |
| [0.0]         |
| [0.8965624]   |
| [0.85604626]  |
| [0.0]         |

Any idea how I can solve this. That is to sum the whole column which is returned.

役に立ちましたか?

解決

Use reduce which is a fold operation:

return p, reduce(sum=0,nn IN relationships(p) |  sum + nn.`c1.mean`) as sum

Values in square brackets are collections/arrays.

他のヒント

First off, given your use of "WITH" and labels, I'm going to assume you're using Cypher 2.x.

Also, to be honest, it's not entirely clear what you're after here, so I'm making some assumptions and stating them.

Second off, some parts of the query are unnecessary. As well, the *1 in your relationship means that there will only be one "hop" in your path. I don't know if that's what you're after so I'm going to make an assumption that you want to possibly go several levels deep (but we'll cap it so as to not kill your Neo4j instance; alternatively, you could use something like "allShortestPaths" but we won't go into that). This assumption can easily be changed by removing the cap. and signifying a single hop.

As for your the results being returned in brackets, extract returns a list of values, potentially only a single one.

So let's rewrite the query a little (note that for ko the identifier is a little confusing above so replace it with whatever you need to).

If we assume that you just want the sum per path, we can do:

MATCH p=(ko:koid {ko: 'K01963'})-[r:abundance*1..5]-(n:Taxon)     
WITH reduce(val = 0, nn IN relationships(p) | val + nn.`c1.mean`) as summed 
RETURN summed;

(This can also be modified to sum over all paths, I believe.)

If we want the total sum of ALL relationships returned, we need something a bit different, and it's even simpler (assuming in this case you really only do have:

MATCH p=(ko:koid {ko: 'K01963'})-[r:abundance]-(n:Taxon)     
RETURN sum(r.`c1.mean`);

Hopefully even if I'm off in my assumptions and how I've read things this will at least get you thinking in the right way.

Mostly, the idea of using paths when you only have 1 hop to make in a path is a bit confusing, but perhaps this will help a little.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top