0

I have three nodes named: A,B and C and there is a relationship between A->B and B->C named 'EDGE', which has a property named cost.

So I am trying to find out the total cost from A to C, using the relationships() function to first find the edges in between and then sum up the cost on edges/relationship using sum() function. So my query becomes:

SELECT * from cypher('demo_graph', $$

      MATCH p = (a:Vertex {name: 'A'})-[:EDGE*]->(c:Vertex {name: 'C'})
      WITH relationships(p) AS edges
      RETURN sum(edge.cost) AS totalCost

$$) as (V agtype);

But this query return the following error:

enter image description here

Do someone know what can be the reason?

Kamlesh Kumar
  • 351
  • 1
  • 7

1 Answers1

0

The reason for this error is that the aggregate function sum() is used to add values of row(s) within a single column but here the relationships(p) returns a list of edges and hence you cannot add the values of this list. One way to do it is to use the UNWIND clause to first break down the list of edges to multiple rows with each row consisting of a single edge and then use the sum() aggregate function on these rows as used here.

SELECT * FROM cypher('demo_graph', $$
MATCH p = (u:Vertex {name : "A"})-[:EDGE*]->(v:Vertex{name : "C"})
UNWIND(relationships(p)) AS x RETURN sum(x) 
$$) AS (result agtype);

PS: previously there was a check for edge type in UNWIND and the following error was displayed

ERROR: UNWIND clause does not support agtype edge

but with the latest commit here, UNWIND can work with edges. You need to pull the recent changes using git pull to use this feature as it recently got merged.

Zainab Saad
  • 728
  • 1
  • 2
  • 8