cancel
Showing results forΒ
Did you mean:Β

## Creating relationship between nodes according to date

Hello everyone! I am beginner in Neo4J. I got 2 nodes Schools and Students and relationships between Students and Schools (Who and Where studied). Now I need to sort studied Students by year. For example(2nd image, I pick Student3 who studied in 2019), if I will pick one Student and see the relations, I need to get his School and Students who studied with him but was one grade higher and lower. Any ideas how to do it?

β

6 REPLIES 6
Ninja

Hi Nural10,
welcome to Neo4j!

I have some questions to your query:

1. Is the year that the students studied in a long?

1. 2. Do you want to return either the students in the prior OR the later year or both?

Assuming the year is an integer and you want to return the students that studied the year prior, this is the query you need:

``````MATCH (student1:Student {name:"Student3"})-[:studied]->(s:School)<-[:studied]-(student2)
WHERE student2.year = student1.year-1
RETURN student2``````

or if you want to see the students that came the year after:

``````MATCH (student1:Student {name:"Student3"})-[:studied]->(s:School)<-[:studied]-(student2)
WHERE student2.year = student1.year+1
RETURN student2``````

or if you want either the prior or the later year students:

``````MATCH (student1:Student {name:"Student3"})-[:studied]->(s:School)<-[:studied]-(student2)
WHERE abs(student2.year - student1.year) = 1
RETURN student2``````

I have one more suggestion: I would put the year a Student started his studies onto the edge and not onto the Student node. If e.g. someone studied in 2 places this modeling would enable you to just add infinitely many schools and years without needing a specific structure on the nodes.

Regards,
Elena

Thanks a lot! I understand the concept.

What about the next case: If the first student studied from 2013 to 2017, and the second studied from 2015 to 2020, how can I set up a relation between them?

And one more.
Such a case, for example:
If a student entered school on a specific date (yyyy-mm-dd),

let's say
start_date = (2009-10-01) and end_date = (2019-03-01), and the second student has the date start_date = (2012-09-13) and end_date = (2017-02-21).

In this case, how can I find a relationship by date between these students?

Hi Nural10,

as for your first question: I would not set up a relationship between them. Of course you can find students that have overlapping study times for a specific school wihtout them having a relationship. Here are some ways on how to do that mathematically: Calculating the overlap of date/time intervals - SAS Users. Taking the third appraoch your cypher query could look like this:

``````MATCH (student1:Student {name:"Student3"})-[rel1:studied]->(s:School)<-[rel2:studied]-(student2)
WHERE min(rel1.endYear, rel2.endYear) - max(rel1.startYear, rel2.startYear) + 1 >= 0
RETURN student2``````

The above will return all students that have studied at the same school as Student3 with overlapping study times. Here, I assume that the endYear and startYear properties are present as longs on the relationships of the students with the school.

If you want to work with more specific dates, you just need to use the data type "Date". Some explanation here: Dates, datetimes, and durations - Developer Guides (neo4j.com). Then, you can use the above query in the same manner.

Regards,
Elena

Thanks a lot!
One more question

This is how my graph looks now.

How can I implement a relationship to check the same start_date and end_date from school's students. (else i wanna cut other relations from this school).

I want to get in final graph::

Hi,

don't you want to try yourself? You start looking from one student. You look at the schools that (s)he has visited and the other students that have visited the same school. And then you're asking whether start and end are equal and if they are great, if they are not, you want to delete the relationship (do you really want to delete out relationships from your graph?). Cypher is a nice query language where you can follow your train of thoughts and just get it into a query.
If you have some queries that are close but just don't work, I am happy to help in correcting them.

Regards,
Elena