Neo4j Cypher: MERGE conditionally with values ​​from LOAD CSV

I am trying to import from CSV where some lines have an account number and some don't. If the accounts have numbers that I would like to combine with them: there will be records in which the name in the account has changed, but the number will remain unchanged. For other entries without an account number, the best I can do is concatenate the account name.

So I really need some kind of conditional: if the line has an account number, merge on it, otherwise merge by the account name. Sort of...

LOAD CSV WITH HEADERS FROM 'file:///testfile.csv' AS line
MERGE (x:Thing {
  CASE line.accountNumber WHEN NULL
    THEN name: line.accountName
    ELSE number: line.accountNumber
  END
})
ON CREATE SET
x.name = line.accountName,
x.number = line.accountNumber

Although, of course, this does not work. Any ideas?

+4
source share
1 answer

"NULL" CSV LOAD CSV, .

TestFile.CSV

acct_name,acct_num
John,1
Stacey,2
Alice,
Bob,4

, ...

LOAD CSV WITH HEADERS FROM 'file:///testfile.csv' AS line

// If acct_num is not null, merge on account number and set name if node is created instead of found.
FOREACH(number IN (CASE WHEN line.acct_num <> "" THEN [TOINT(line.acct_num)] ELSE [] END) |
    MERGE (x:Thing {number:number})
    ON CREATE SET x.name = line.acct_name
)

// If acct_num is null, merge on account name. This node will not have an account number if it is created instead of matched.
FOREACH(name IN (CASE WHEN line.acct_num = "" THEN [line.acct_name] ELSE [] END) |
    MERGE (x:Thing {name:name})
)
+10

All Articles