Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
09-08-2022 04:12 AM - edited 09-08-2022 04:15 AM
Hi,
There is a graph with nodes "City" :
CREATE (a:City {DS: 'AAA'})
CREATE (b:City {DS: 'BBB'})
CREATE (c:City {DS: 'CCC'})
CREATE (d:City {DS: 'DDD'})
CREATE (e:City {DS: 'EEE'})
CREATE (f:City {DS: 'FFF'})
CREATE (g:City {DS: 'GGG'})
The relatioships should be extracted from a csv, content:
route, DS
66,AAA
66,BBB
66,CCC
66,DDD
77,EEE
77,FFF
77,GGG
and creted the relationships as follows:
The question: How to build relationships based on different (x, x+1) rows ?
Thanks a lot in advance,
Michel
The code for creating the relationships above:
MATCH (p1:City {DS: 'AAA'}), (p2:City {DS: 'BBB'})
CREATE (p1)-[:NB {route: '66'}]->(p2)
MATCH (p1:City {DS: 'BBB'}), (p2:City {DS: 'CCC'})
CREATE (p1)-[:NB {route: '66'}]->(p2)
MATCH (p1:City {DS: 'CCC'}), (p2:City {DS: 'DDD'})
CREATE (p1)-[:NB {route: '66'}]->(p2)
MATCH (p1:City {DS: 'EEE'}), (p2:City {DS: 'FFF'})
CREATE (p1)-[:NB {route: '77'}]->(p2)
MATCH (p1:City {DS: 'FFF'}), (p2:City {DS: 'GGG'})
CREATE (p1)-[:NB {route: '77'}]->(p2)
Solved! Go to Solution.
09-08-2022 07:02 AM
load csv with headers from "file:///Book1.csv" as row
with collect(row) as rows
unwind range(0,size(rows)-2) as i
match(a:City{DS:rows[i].DS})
match(b:City{DS:rows[i+1].DS})
merge(a)-[r:REL]->(b)
set r.route = rows[i].route
09-08-2022 07:21 AM - edited 09-08-2022 08:38 AM
Hi @glilienfield ,
many thanks ! The relationship betwen DDD and EEE doesn't exist. Have you any idea, how to avoid creating of such relationships ?
Thanks,
Michel
09-08-2022 08:43 AM
You could place a '-' character in the route column to indicate to skip the relationship.
09-08-2022 09:25 AM
09-08-2022 07:02 AM
load csv with headers from "file:///Book1.csv" as row
with collect(row) as rows
unwind range(0,size(rows)-2) as i
match(a:City{DS:rows[i].DS})
match(b:City{DS:rows[i+1].DS})
merge(a)-[r:REL]->(b)
set r.route = rows[i].route
09-08-2022 07:21 AM - edited 09-08-2022 08:38 AM
Hi @glilienfield ,
many thanks ! The relationship betwen DDD and EEE doesn't exist. Have you any idea, how to avoid creating of such relationships ?
Thanks,
Michel
09-08-2022 08:43 AM
You could place a '-' character in the route column to indicate to skip the relationship.
09-08-2022 09:15 AM - edited 09-08-2022 09:15 AM
Hi @glilienfield , for that I have to write a VBA code (nodes > 20000, routes > 1000) . In the meanwhile I did it. The only open question for further similary tasks: How can I compare and reference values from different rows (i, i+1)
Something like this:
CASE
WHEN rows[i].DS = rows[i+1].DS THEN ...
END AS ..
09-08-2022 09:25 AM
You can alter the code I provided to do that.
All the sessions of the conference are now available online