Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
12-21-2019 12:26 PM
Hi,
I'm trying to figure out what I assume is a very simple cypher code. I have a list of companies, a list of codes and a list of departments. I use merge for companies and codes, but I would like for the departments to be unique for each code even though they repeat, my data looks like this:
Company A Code 1 Finance
Company A Code 1 Finance
Company A Code 1 Finance
Company A Code 1 Sales
Company A Code 1 HR
Company B Code 11 Finance
Company B Code 12 Sales
Company B Code 13 HR
I would like the data model to look like this:
I've tried a variation of WITH and MATCH but can't get exactly that data model:
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM 'file:///Output.csv' as line
MERGE (c:Company { name: toUpper(line.Company
) })
MERGE (co:Code { name: toUpper(line.Code
) })
MERGE (c)-[:has_code]->(co)
WITH c,co
CREATE (dep:Department { name: toUpper(line.Department
) })
MERGE (dep)-[:belongs_to]->(co)
Solved! Go to Solution.
12-22-2019 03:35 AM
Hi arunas,
Output.csv
Company,Code,Department
Company A,Code 1,Finance
Company A,Code 1,Finance
Company A,Code 1,Finance
Company A,Code 1,Sales
Company A,Code 1,HR
Company B,Code 11,Finance
Company B,Code 12,Sales
Company B,Code 13,HR
I changed your Cypher code to look like this.
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM 'file:///Output.csv' as line
MERGE (c:Company { name: toUpper(line.Company) })
MERGE (co:Code { name: toUpper(line.Code) })
MERGE (c)-[:HAS_CODE]->(co)
MERGE (dep:Department { name: toUpper(line.Department) })-[:BELONGS_TO]->(co)
As a result, the Finance of Code1 of Company is one.
Is this image the same as your data model?
12-21-2019 05:40 PM
Can you describe how the output you are seeing varies from your desired result?
12-22-2019 01:17 AM
Hi,
When I use merge I get this
And with Create I get this:
Basically, the way I see the code it should be something like ON CREATE project CREATE UNIQUE department.
12-22-2019 03:35 AM
Hi arunas,
Output.csv
Company,Code,Department
Company A,Code 1,Finance
Company A,Code 1,Finance
Company A,Code 1,Finance
Company A,Code 1,Sales
Company A,Code 1,HR
Company B,Code 11,Finance
Company B,Code 12,Sales
Company B,Code 13,HR
I changed your Cypher code to look like this.
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM 'file:///Output.csv' as line
MERGE (c:Company { name: toUpper(line.Company) })
MERGE (co:Code { name: toUpper(line.Code) })
MERGE (c)-[:HAS_CODE]->(co)
MERGE (dep:Department { name: toUpper(line.Department) })-[:BELONGS_TO]->(co)
As a result, the Finance of Code1 of Company is one.
Is this image the same as your data model?
12-22-2019 03:53 AM
Hi Koji,
That's exactly what I wanted. Thank you!
All the sessions of the conference are now available online