dhanushkac
dhanushkac

Reputation: 550

Use one query value in another query Neo4j CQL

I am trying to make database of Bookstore where customers can buy books. There is lables Book, BookCategory, Customer, Invoice. I am using relation INVOICEITEM between invoice and books. i added property netTotal in invoice to store the sum value of books related in INVOICEITEM with invoice. I tried to get total value of books which is related to invoice.

MATCH (n:Invoice {id:'inv001'})-[:INVOICEITEM]->m RETURN SUM(m.price)

and i tried to change netTotal propery of invoice by using this command.

MATCH (n:Invoice{ id: 'inv001' }) SET n.netTotal = ((n:Invoice {id:'inv001'})-[:INVOICEITEM]-> m RETURN SUM(m.price))

but it returns an error. I am new to neo4j and thank you.

Upvotes: 0

Views: 729

Answers (1)

William Lyon
William Lyon

Reputation: 8546

The query to update netTotal on the Invoice node should be:

MATCH (n:Invoice {id: 'inv001'})-[:INVOICEITEM]->(m:Book) 
WITH n, SUM(m.price) AS netTotal
SET n.netTotal = netTotal

Calculate the sum of the Book prices using a WITH statement to bring that value along to a SET statement to update the netTotal property on Invoice n. Note that you also need to specify n in the WITH statement to bring that bound variable through the query as well. More info on WITH statement here

Upvotes: 2

Related Questions