cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

cartesian results

Avi
Node Clone

I successfully ran a simple optimization query for a single project: ‘Project A’ and got 2 set of lists

List 1: Employee label {Employee A, Employee B, Employee C}

List 2: Relationship property Hours {50,60,10}

My objective is to create relationship between Project and Employee and set Hours as a property.

Employee A will_work 50 hours on Project A

Employee B will_work 60 hours on Project A

Employee C will_work 10 hours on Project A

 

Without success I tried nested FOREACH just to receive a cartesian results (9 options).

1 ACCEPTED SOLUTION

That can be several approaches to solve this depending on how your are setting up the data.  He is an example of a way to approach it.  Do you have your entire query that we can look at to modify it?  Do you intend to read the data from a spreadsheet?

with ["A", "B", "C"] as employees, [50, 60, 10] as hours
unwind range(0,size(employees)-1) as index
merge(e:Employee{name:employees[index]})
merge(p:Project{name:"A"})
merge(e)-[r:WILL_WORK]->(p)
SET r.hours=hours[index]
return e, p

Screen Shot 2022-10-20 at 1.01.09 PM.png 

View solution in original post

4 REPLIES 4

You can get the Cartesian product between two lists with a double unwind, as follows:

with ["A", "B", "C"] as employees, [50, 60, 10] as hours
unwind employees as employee
unwind hours as hour
return employee, hour

 Are you looking to create those three relationships from list1 and list2? 

Thank you again for taking the time, after rereading my request I realized that I didn't do a good job describing the issue. 

My objective is to avoid a cartesian query and unwind the two lists and create relationship.
the desired result are 3 relationships (not 9) :

Employee A will_work 50 hours on Project A
Employee B will_work 60 hours on Project A
Employee C will_work 10 hours on Project A

this is a "test" that I ran without success to set a property (Temp) on Employee label 

WITH "Employee A,Employee B,Employee C" as vEmployee, "50,60,10" as vHRS
FOREACH (name IN split(vEmployee, ",")|FOREACH (hrs IN split(vHRS, ",")|
MERGE (n:Employee {ID:name})
SET n.Temp=hrs))

so to summarize I need to  unwind two coordinated list and create  3 relationships and set accordingly Hours. 
(Employee)-[will_work]->(Project A)
SET Hours=vHRS

That can be several approaches to solve this depending on how your are setting up the data.  He is an example of a way to approach it.  Do you have your entire query that we can look at to modify it?  Do you intend to read the data from a spreadsheet?

with ["A", "B", "C"] as employees, [50, 60, 10] as hours
unwind range(0,size(employees)-1) as index
merge(e:Employee{name:employees[index]})
merge(p:Project{name:"A"})
merge(e)-[r:WILL_WORK]->(p)
SET r.hours=hours[index]
return e, p

Screen Shot 2022-10-20 at 1.01.09 PM.png 

This is amazing , thank you for taking the time.