I'm new to neo4j

hi,
i'm new to neo4j i would like to know more about foreach loop and dynamic relationship.
I was working with a csv file which has duplication and null values.


i'm trying to get this o/p. But with the query
load csv with headers from "file:///newtask.csv" as row
WITH row WHERE NOT row.E IS null
WITH row WHERE NOT row.A IS null
MERGE(p1:person{firstName:row.A,age:row.E})
MERGE(p2:person{firstName:row.B})
WITH p1, p2, row
CALL apoc.create.relationship(p1, row.C,{number:row.D}, p2) YIELD rel
RETURN rel

I made this but still one node is missing. So i tried foreach loop but this throws error.
load csv with headers from "file:///newtask.csv" as row
FOREACH(ignoreMe IN CASE WHEN row.A IS NOT NULL THEN [1] ELSE END | MERGE(p1:person{firstName:row.A,age:row.E}));
FOREACH(ignoreMe IN CASE WHEN row.E IS NOT NULL THEN [1] ELSE END | MERGE(p2:person{firstName:row.B,age:row.E}));
WITH p1, p2, row
CALL apoc.create.relationship(p1, row.C,{number:row.D}, p2) YIELD rel
RETURN rel

I'm stuck here.

CSV file I'm working on ->
true

This seems to work. I returned something because I was forced to return a value from the last call subquery.

load csv with headers from "file:///people.csv" as row
call {
    with row
    with row where row.A is not null
    merge(n:person{name:row.A})
    set n.age = row.E
}
call {
    with row
    with row where row.B is not null
    merge(n:person{name:row.B})
}
call{
    with row
    with row where row.A is not null and row.B is not null
    match(a:person{name:row.A}),(b:person{name:row.B})
    CALL apoc.create.relationship(a, row.C,{number:row.D}, b) yield rel
    return 1 as rel
}
return count(rel) as cnt_rel

1 Like

[quote="sreeraj_sj, post:4, topic:60656, full:true"]
thank you this worked and it was easy to understand.
by the way i have 1 doubt why we are returning 1 as rel here ? -->return 1 as rel

and could you please explain me this ---> return count(rel) as cnt_rel
thanks in advance.

I got an error without the return inside the call subquery. The complaint is you can end a query will a call statement. As such, I need to return something. Yes, you could return the ‘rel’ created by the ‘call’. I decided to return the number of relationships created so I can verify the correct number was made. To do this I return a value of one from the subquery. It could be any value or the relationship itself. This will return one only when a relationship is created. Then I count the number ones returned, thus the number of relationships created in the call subquery.

1 Like

helpful! thank you for your response. @glilienfield

1 Like