Creating NEW relationships among nodes' neighbours

Hi,

I have a graph containing neighbours, that is, structured in a star type schema in the language of DW modelling("Account" nodes having multiple "Fraud" nodes attached to them). I want to create a further relationship among Fruds nodes associated with an account in ascending date wise i.e for an (Account)-[:CONDUCTS]-> (Fraud)-[:NEXT]->(Next_Fraud), Now from the existing graph, I am trying to run the following query. My idea of structuring the query is; 1)select all accounts 2) pick up one account at a time 3) picks up its associated frauds 4) sort frauds according to commit date 5) create a relationship among frauds [:NEXT]

match (a:Account) with collect(a) as accts unwind(accts) as accounts match (accounts)-[c:CONDUCTING]->(f:Fraud) with f order by f.date with collect(f) as ff foreach (i in range(0,size(ff)-2)| foreach (fraud in [ff[i]] | foreach(next_fraud in [ff[i+1]] | merge (fraud)-[:NEXT]->(fraud_next) ) ) )

The above query does not produce anything. On running EXPLAIN or PROFILE, I see that in second match (match (accounts)-[c:CONDUCTING]->(f:Fraud), query is selecting all records, not the frauds of a one node at a time). Need help how would I achieve my object?

Try this:

MATCH (a:Account)-[:CONDUCTS]-> (b:Fraud)
WITH b order by b.commitdate ASC
WITH COLLECT(b) as b1
CALL apoc.nodes.link(b1, 'NEXT')
RETURN b1

Thanks @ameyasoft for help. I tried your suggested solution it worked but sometimes it created a linked list of Fraud nodes by disregarding the Account node. For example, the last fraud of Account 1 's Frauds linked list is connected to some Fraud node of another, say, Account 2 Fraud linked list which is just greater than in date. So in attached picture, the red circle NEXT relationship should not be created. Like I mention, it is happening on some accounts. Not sure what is the reason for it.

I think I found a solution by tweaking your suggested solution. Basically, I used a CALL statement to run a subquery on an individual Account node:

match (a:Account) call {with a match (a)-[:CONDUCTING]->(f:Fraud) with f order by f.commit_date asc with collect(f) as fraud call apoc.nodes.link(fraud,'NEXT1') return fraud} return a,fraud