# How to model relationships between two entities that change over time?

**URL:** <https://community.neo4j.com/t/how-to-model-relationships-between-two-entities-that-change-over-time/5514>\
**Category:** Modeling\
**Created:** [March 6, 2019, 1:40pm UTC](https://community.neo4j.com/t/how-to-model-relationships-between-two-entities-that-change-over-time/5514 "2019-03-06T13:40:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben\_Baert](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ben_baert/32/3572_2.png) [@Ben\_Baert](https://community.neo4j.com/u/Ben_Baert)\
**Post date:** [March 6, 2019, 1:40pm UTC](https://community.neo4j.com/t/how-to-model-relationships-between-two-entities-that-change-over-time/5514/1 "2019-03-06T13:40:11Z")

</div>

I am creating a database that maps relationships between entities. These relationships change over time.

For example, methane emissions are responsible for x% of greenhouse gas emissions in 2017 and y% of greenhouse gas emissions in 2018.

Without taking time into account, it could look something like this:

[![](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/3/3ea30a4b3ad161b205a717a365c6ae90b0b66be8.png) ](https://dl.dropboxusercontent.com/s/trg0cr7pyy8ubv8/Screenshot%202019-03-06%2014.27.44.png?dl=0)

I do however, want to track changes over time. My preference would be to link a relationship to a node, but this is not possible. I have two ideas in my mind.

The first is to simply create multiple relationships between the two entities, one for each year that I'm tracking:

[![](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/8/813352c0a4131f9a6921236adbbde8eee5deab07.png) ](https://dl.dropboxusercontent.com/s/7kpi8w3uwejgsa2/Screenshot%202019-03-06%2014.32.09.png?dl=0)

The other is to create a proper time tree and make a node for each year, like so:

[![](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/0/00fb0d378478621bb77d96f1b49557bbd1511c0b.png) ](https://dl.dropboxusercontent.com/s/7gnj4uio8nkorh7/Screenshot%202019-03-06%2014.37.18.png?dl=0)

I'm not really sure which one to pick (or whether there is another, more sensible alternative). I tend to like the latter option with the intermediate node a little bit better, but it also introduces complexity that the simpler solution avoids.

Most articles on this topic are about nodes that are connected to a time tree, in which case it's fairly straightforward. Here it's the relationship that should be connected to the time tree, but that's not possible.

---

<div class="post-metadata">

**Author:** ![dominicvivek06](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dominicvivek06/32/5034_2.png) [@dominicvivek06](https://community.neo4j.com/u/dominicvivek06)\
**Post date:** [March 6, 2019, 1:57pm UTC](https://community.neo4j.com/t/how-to-model-relationships-between-two-entities-that-change-over-time/5514/2 "2019-03-06T13:57:51Z")

</div>

can you share 25% of the sample data? let me see the cardinality / distribution of the data.

---

<div class="post-metadata">

**Author:** ![Ben\_Baert](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ben_baert/32/3572_2.png) [@Ben\_Baert](https://community.neo4j.com/u/Ben_Baert)\
**Post date:** [March 6, 2019, 2:02pm UTC](https://community.neo4j.com/t/how-to-model-relationships-between-two-entities-that-change-over-time/5514/3 "2019-03-06T14:02:00Z")

</div>

There is no sample data yet.

Nodes would likely have a high number of incoming and outgoing relationships (so high cardinality).

---

<div class="post-metadata">

**Author:** ![david.fauth](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/david.fauth/32/189_2.png) [@david.fauth](https://community.neo4j.com/u/david.fauth)\
**Post date:** [March 6, 2019, 11:26pm UTC](https://community.neo4j.com/t/how-to-model-relationships-between-two-entities-that-change-over-time/5514/4 "2019-03-06T23:26:32Z")

</div>

You can use a specific relationship type to help traverse.  
Example: 2017\_CONTRIBUTE\_TO and 2018\_CONTRIBUTE\_TO

---

<div class="post-metadata">

**Author:** ![vit.mareev](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/vit.mareev/32/3421_2.png) [@vit.mareev](https://community.neo4j.com/u/vit.mareev)\
**Post date:** [March 8, 2019, 1:02pm UTC](https://community.neo4j.com/t/how-to-model-relationships-between-two-entities-that-change-over-time/5514/5 "2019-03-08T13:02:44Z")

</div>

We are using something like that: [Neo4j Versioner Core Documentation | neo4j-versioner-core](https://h-omer.github.io/neo4j-versioner-core/)

---

<div class="post-metadata">

**Author:** ![mmuthu](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/mmuthu/32/5003_2.png) [@mmuthu](https://community.neo4j.com/u/mmuthu)\
**Post date:** [June 8, 2019, 6:25am UTC](https://community.neo4j.com/t/how-to-model-relationships-between-two-entities-that-change-over-time/5514/6 "2019-06-08T06:25:12Z")

</div>

Ben,  
Did you get any solution? I am interested to hear as well.

Thanks,  
Mahendar

---

<div class="post-metadata">

**Author:** ![ameyasoft](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ameyasoft/32/32639_2.png) [@ameyasoft](https://community.neo4j.com/u/ameyasoft)\
**Post date:** [June 8, 2019, 8:51am UTC](https://community.neo4j.com/t/how-to-model-relationships-between-two-entities-that-change-over-time/5514/7 "2019-06-08T08:51:48Z")

</div>

Here is my solution:

Cypher script:

MERGE (g:Emissions {name: "Methane"})  
MERGE (p:Percentage {percentage: "25", date: "01/01/2019"})  
MERGE (e:Effect {name: "Greenhouse Gases"})

MERGE (pr:PrevPercentage {percentage: "20", date: "11/01/2018"})  
MERGE (pr1:PrevPercentage {percentage: "15", date: "01/01/2018"})

MERGE (g)-[:PERCENTAGE]-\>(p)  
MERGE (p)-[:CONTRIBUTES\_TO]-\>(e)  
MERGE (p)-[:PREVIOUS]-\>(pr)  
MERGE (pr)-[:PREVIOUS]-\>(pr1)  
;

Result:  
 ![ben1](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/a/a4225e7b131f1331c69d3d9f5296de75a96eb27b.png)

Analysis:

MATCH (e:Emissions)-[:PERCENTAGE]-\>(p:Percentage)-[:CONTRIBUTES\_TO]-\>(g:Effect)  
OPTIONAL MATCH (p)-[:PREVIOUS\*]-(h:PrevPercentage)  
RETURN e.name, g.name,p.percentage as Current, p.date, h.percentage as Prev, h.date;

 ![ben2](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/0/0c4d2aa300e38175b0ec363aaf08c9b4ce6afbd2.png)

Percentage at a specified date:  
MATCH (e:Emissions)-[:PERCENTAGE]-\>(p:Percentage)-[:CONTRIBUTES\_TO]-\>(g:Effect)  
OPTIONAL MATCH (p)-[:PREVIOUS\*]-(h:PrevPercentage)  
WHERE h.date = "01/01/2018"  
RETURN e.name, g.name,p.percentage as Current, p.date, h.percentage as Prev, h.date;

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