Reputation: 715
I have a graph that contains two kinds of nodes: one is to represent customers and other for merchants and stores. The graph keeps track on where and how much the customer spent on a store, representing this interaction with edges. Here is how I'm creating them:
-- Customers
CREATE (Bobby :Person {id:'1', name:'Bobby', gender:'man', age: 72})
-- Merchants
CREATE (Amazon :Merchant {id:'2', name:'Amazon', street:'2626 Wilkinson Court', address:'San Bernardino, CA 92410'})
-- Transaction
CREATE (Bobby)-[:HAS_BOUGHT_AT {amount:'986', time:'4/17/2014', status:'Undisputed'}]->(Amazon)
When I want to retrieve only the dates of the purchases in descending order, it successfuly returns them:
SELECT * FROM cypher('MyGraph', $$
MATCH (v)-[r :HAS_BOUGHT_AT]->(m)
WITH toInteger(split(r.time, '/')[0]) as month,
toInteger(split(r.time, '/')[1]) as day,
toInteger(split(r.time, '/')[2]) as year
RETURN month, day, year
ORDER BY year DESC, month DESC, day DESC
$$) AS (month agtype, day agtype, year agtype);
month | day | year
-------+-----+------
12 | 28 | 2014
12 | 26 | 2014
12 | 24 | 2014
12 | 23 | 2014
12 | 20 | 2014
12 | 20 | 2014
12 | 20 | 2014
12 | 20 | 2014
12 | 16 | 2014
12 | 15 | 2014
11 | 28 | 2014
11 | 27 | 2014
11 | 14 | 2014
10 | 25 | 2014
10 | 15 | 2014
But if I want to show the names of the vertices that are being matched, it throws an error instead.
SELECT * FROM cypher('MyGraph', $$
MATCH (v)-[r :HAS_BOUGHT_AT]->(m)
WITH toInteger(split(r.time, '/')[0]) as month,
toInteger(split(r.time, '/')[1]) as day,
toInteger(split(r.time, '/')[2]) as year
RETURN month, day, year, v.name, m.name
ORDER BY year DESC, month DESC, day DESC
$$) AS (month agtype, day agtype, year agtype, v_name agtype, m_name agtype);
ERROR: no relation entry for relid 3
Upvotes: 0
Views: 126
Reputation: 230
The reason you are getting this error is because you are not passing on v and m. So you cannot access them after WITH.
The below query resolves your error.
SELECT * FROM cypher('graph', $$
MATCH (v)-[r :HAS_BOUGHT_AT]->(m)
WITH v,m,toInteger(split(r.time, '/')[0]) as month,
toInteger(split(r.time, '/')[1]) as day,
toInteger(split(r.time, '/')[2]) as year
RETURN month, day, year, v.name, m.name
ORDER BY year DESC, month DESC, day DESC
$$) AS (month agtype, day agtype, year agtype, v_name agtype, m_name agtype);
Result:
month | day | year | v_name | m_name
-------+-----+------+---------+----------
4 | 17 | 2014 | "Bobby" | "Amazon"
(1 row)
Upvotes: 1