1
votes

I have the below graph in Neo4j. In the below graph Subscriber are the entities who has performed different actions at different time. Actions are the relationships in the graph (Complained, Recharged, Enquired, Clicked).

My requirement is to write the Cypher query to get all the next action of all the Subscriber (example: I want the query to find what is the next action performed by all the Subscriber who has done Recharged action with RechargeType=TT. I need to fetch the next action based on the time (ActionTime)

I am also providing the link to get the entire data of my graph in the .xls returned after writing the below query.

Graph Data

match(n)-[r]->(k)
return n, r, type(r), k

Below query return the details of all the Subscriber whose RechargeType='TT'. What I need is the cypher query to get the immediate next action taken by these Subscriber based on the 'ActionTime' and grouping them as per the action taken(Recharged,Clicked,Complained,Enquired).

Query: match(n:Subscriber)-[r:Recharged]->(k) where r.RechargeType='TT' return n, r, type(r), k;

enter image description here

Please let me know if any further details/explanation is required for my question to be answered..

Edit: Graph data has been modified making the Action time as same for all actions performed (relationships in this case) and also the modified graph image has been uploaded.

Graph

1
What you mean with "fetch the next action based on the time (eg. RechargeTime, CallTime))"? Also, what have you tried so far? Thanks!Bruno Peres
I mean to ask the immediate next action performed by the Subscribers and grouping it as per the action performed (relationships). Immediate next action need to be found by using the 'Time' property of the relationship (eg. CallTime,RechargeTime). Please refer the attached graph data.palpras
Is not viable to you rename all the time properties to Time instead of RechargeTime, CallTime, etc? I think it can be simplify your queries... Also, the "action type" is stored in the relationship (Recharged, Clicked, etc).Bruno Peres
@BrunoPeres Yeah agreed, all the 'Time' property if renamed will simplify the query. So lets assume all the time property as 'Time'. Recharged, Clicked, etc. are type of relationship and I am not storing it in relationship, if required I can do that as well. I am new to Neo4j so not sure about the best approach. Let me know if I need to store the action type, if yes, then what will be the query to meet my requirement. Thanks!!palpras
@BrunoPeres Edited the question as per your suggestion. Got any idea on the approach and query.palpras

1 Answers

0
votes

Below query is the answer to my question.

MATCH (n) with n, collect(n.msisdn) as list 
Match (n) WHERE n.msisdn IN list
MATCH (n)-[r]->(k) WHERE r.ActionType='TT'
WITH n, MIN(r.ActionTime) as min
MATCH (n)-[rout]->() WHERE rout.ActionTime > min
WITH n, rout
ORDER BY rout.ActionTime
WITH n, COLLECT(rout)[0] AS r
RETURN {
node: n,
outgoing:
  { relationship: TYPE(r), 
    node: ENDNODE(r),
    rType: r.ActionType,
    rTime: r.ActionTime,
    rChannel: r.ContactChannel
  }
  } AS result;