Passing the output of one query as an input to another query (in the same Cypher)

Hi All,

I am new to Neo4j. Please excuse me playing dumb here.
I wanted to pass the output of the below query which returns the last date into the where clause of another query. Kindly help in how to achieve this

MATCH (c:Customer)-[:OWNS]->(a:Account) OPTIONAL MATCH (a)-[:PURCHASED]->(:Fund)-[:HAS_DAILY_CLOSE]->(fc:DayClose) WITH c, a, MAX(fc.date) AS last_date WHERE last_date IS NOT NULL RETURN DISTINCT last_date;

You want it all in one cypher query, or you are executing this using a driver and want to chain the queries in one transaction?

If in one cypher query, can you provide the rest of the query?

As a start, you can change the “return” to a “with” and add the rest of the query. You will be passing rows of dates to the rest of the query, so the rest of the query should be dependent on the last date. You will get results for each distinct date.

1 Like

Hi @glilienfield , thank you so much for the response. I found the solution for this.

MATCH (d:DayClose)
WITH d
ORDER BY d.date DESC
LIMIT 1
WITH d.date AS lastDate
MATCH (c:Customer)-[:OWNS]->(a:Account)
OPTIONAL MATCH (a)-[p:PURCHASED]->(f:Fund)-[:HAS_DAILY_CLOSE]->(fc:DayClose)
WHERE fc.date = date(lastDate)
WITH c, a, f, COLLECT(fc) AS fundCloses,p
UNWIND fundCloses AS fc
RETURN c.owner_name AS owner_name, a.account_type AS account_type,
fc.ticker AS ticker,ROUND(SUM(p.number_of_shares * fc.close),2) as value, MAX(fc.date) AS last_date
ORDER BY owner_name asc, account_type asc,ticker asc ;