# How to create a unique node based on another node

**URL:** <https://community.neo4j.com/t/how-to-create-a-unique-node-based-on-another-node/13063>\
**Category:** Cypher\
**Created:** [December 21, 2019, 8:26pm UTC](https://community.neo4j.com/t/how-to-create-a-unique-node-based-on-another-node/13063 "2019-12-21T20:26:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arunas.kraujutis](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/arunas.kraujutis/32/4694_2.png) [@arunas.kraujutis](https://community.neo4j.com/u/arunas.kraujutis)\
**Post date:** [December 21, 2019, 8:26pm UTC](https://community.neo4j.com/t/how-to-create-a-unique-node-based-on-another-node/13063/1 "2019-12-21T20:26:45Z")

</div>

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:

 ![Annotation](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/e/e4bcb76e80af2d3c3c9cbeada714ab68fe1e21fa.png)

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)

---

<div class="post-metadata">

**Author:** ![mckenzma](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/mckenzma/32/78_2.png) [@mckenzma](https://community.neo4j.com/u/mckenzma)\
**Post date:** [December 22, 2019, 1:40am UTC](https://community.neo4j.com/t/how-to-create-a-unique-node-based-on-another-node/13063/2 "2019-12-22T01:40:06Z")

</div>

Can you describe how the output you are seeing varies from your desired result?

---

<div class="post-metadata">

**Author:** ![arunas.kraujutis](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/arunas.kraujutis/32/4694_2.png) [@arunas.kraujutis](https://community.neo4j.com/u/arunas.kraujutis)\
**Post date:** [December 22, 2019, 9:17am UTC](https://community.neo4j.com/t/how-to-create-a-unique-node-based-on-another-node/13063/3 "2019-12-22T09:17:09Z")

</div>

Hi,

When I use merge I get this

 ![Annotation2](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/f/f95b4f56cce92964101de01385211be0990e6841.png)

And with Create I get this:  
 ![Annotation3](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/e/eb4128887f9f1608e946d3e2877074cc027bfb28.png)

Basically, the way I see the code it should be something like ON CREATE project CREATE UNIQUE department.

---

<div class="post-metadata">

**Author:** ![koji](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/koji/32/27564_2.png) [@koji](https://community.neo4j.com/u/koji)\
**Post date:** [December 22, 2019, 11:35am UTC](https://community.neo4j.com/t/how-to-create-a-unique-node-based-on-another-node/13063/4 "2019-12-22T11:35:04Z")

</div>

Hi arunas,

Output.csv

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

```auto
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?

 ![uniquenode](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/0/06c6def485340c6c7d5601dcc9d6ccbada31bffe.jpeg)

---

<div class="post-metadata">

**Author:** ![arunas.kraujutis](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/arunas.kraujutis/32/4694_2.png) [@arunas.kraujutis](https://community.neo4j.com/u/arunas.kraujutis)\
**Post date:** [December 22, 2019, 11:53am UTC](https://community.neo4j.com/t/how-to-create-a-unique-node-based-on-another-node/13063/5 "2019-12-22T11:53:06Z")

</div>

Hi Koji,

That's exactly what I wanted. Thank you!
