Persons and tables in a session

(Kasimir Mirage) #1

I asked this question on stackoverflow and they offered me to try your community. Please refer to the problem description:

(Ameyasoft) #2


Let us assume that people P1 and P2 sat at table T2 in session 1. Now you want to find out if P1 and P2 sit in other tables in other sessions.

I created a sample scenario with 7 people and 4 tables. 3 tables has two chairs each and the 4th table has one chair:

Cypher query to find the other tables/sessions where P1 and P2 sat:

MATCH (c:Person)-[:SITS_ON {session:1}]->(d:Table)
OPTIONAL MATCH (c)-[r:SITS_ON]->(e:Table)
WHERE <> "T2"
RETURN c,d,e;


The result shows that P1 and P2 sat at tables T1 and T3 besides T2.
Hope this helps!


(Kasimir Mirage) #3

Thanks. This was not the final response I need but it helps. Let me understand how the query you wrote gives that result. Because the query says that select the persons that sat in session 1 on table 2 (the first part) then on second part it says between the first part's result set select the persons who sat on a table other than t2 on any session. (However two persons may visit each other on table 2 in session 1 and again in table 2 in session 2, but now this is not the matter). Suppose that p1 and p2 visit each other on table 2 and session 1. On session 2 p1 sits on table 1 and p2 sits on table 3. The above query returns p1, p2 , however they did not encounter each other again.

(Ameyasoft) #4

I think you can achieve whatever you want if you can change the relationship type. I do not know if this database is in production or in development stage.

I use the same 7 people and 4 tables. I assume this arrangement of people and tables.

People and Tables are linked by the relationship 'SESSION1' for session1, 'SESSION2' for session2 and 'SESSION3' for session 3. Here is the Cypher query to generate:

CREATE(p:Person {name:"P1"}), (p1:Person {name:"P2"}), (p2:Person {name:"P3"}),
(p3:Person {name:"P4"}), (p4:Person {name:"P5"}), (p5:Person {name:"P6"}), (p6:Person {name:"P7"})
CREATE (t:Table {name:"T1"}), (t1:Table {name:"T2"}), (t2:Table {name:"T3"}), (t3:Table {name:"T4"})
CREATE (p)-[:SESSION1]->(t), (p1)-[:SESSION1]->(t1), (p2)-[:SESSION1]->(t2), (p3)-[:SESSION1]->(t3)
CREATE (p6)-[:SESSION1]->(t), (p5)-[:SESSION1]->(t1), (p4)-[:SESSION1]->(t2)
CREATE (p3)-[:SESSION2]->(t), (p2)-[:SESSION2]->(t1), (p1)-[:SESSION2]->(t2), (p)-[:SESSION2]->(t3)
CREATE (p4)-[:SESSION2]->(t), (p6)-[:SESSION2]->(t1), (p5)-[:SESSION2]->(t2)
CREATE (p1)-[:SESSION3]->(t), (p)-[:SESSION3]->(t1), (p3)-[:SESSION3]->(t2), (p2)-[:SESSION3]->(t3)
CREATE (p5)-[:SESSION3]->(t), (p4)-[:SESSION3]->(t1), (p6)-[:SESSION3]->(t2);


Select people sat at table 'T3' in session 1 and check to see if they both sat together at the other tables in other sessions. Cypher query:

MATCH (c:Person)-[:SESSION1]->(d:Table)
OPTIONAL MATCH (c)-[r]->(e:Table)
WHERE <> "T3"
RETURN c,d,e;

This shows P2 and P6 sat together at tables T1 and T2 too.

Repeat the above query for table 'T1' and the result is:

This shows that P1 and P7 sat together at table T1 in session 1 only and all other sessions they were sitting at different tables.


(Kasimir Mirage) #5

Thank you. But I think it will not work. First, in the example you provided, in session 1 the p2 and p4 are together on table 3 not p2 and p6.

Your query is like this:

in first part it says select all persons who sat on table T3 in session 1 and in second part it says: among these selected persons select who sits on any table other than T3 and in any session. (this includes session1 again but for now I ignore this fault)

Imagine that p3 and p5 visit together in session 1 on table 2. At session 2, p3 sits on table 1 and p5 on table 3, so they dont visit each other, but your query will return p3 and p5 because they meet the query.