cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Reading csv with comparing different rows

Michel
Node Clone

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'})

Michel_0-1662634870946.png

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:

Michel_1-1662635381240.png

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)

 

 

 

 

 

 

4 ACCEPTED SOLUTIONS

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

Screen Shot 2022-09-08 at 10.00.20 AM.png

Screen Shot 2022-09-08 at 10.01.33 AM.png

View solution in original post

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

View solution in original post

You could place a '-' character in the route column to indicate to skip the relationship.  

View solution in original post

5 REPLIES 5

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

Screen Shot 2022-09-08 at 10.00.20 AM.png

Screen Shot 2022-09-08 at 10.01.33 AM.png

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

You could place a '-' character in the route column to indicate to skip the relationship.  

Michel
Node Clone

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 ..

 

 

You can alter the code I provided to do that.