MATCH (n { EmpId: '1234' })
SET n: Accountant
RETURN n.name , n.EmpId , n.DepartmentId
but i want to set the label as Accountant only if the department id is matching to some value.
MATCH (n { EmpId: '1234' })
SET n: (CASE when DepartmentId='100' then Accountant ELSE END)
RETURN n.name , n.EmpId , n.DepartmentId
I can set some property as true and false in and then in next query i can set it only if its true but i don't want to create that way. Need in one query like above. Could this be done?
Thanks Thomas for your response!
I tried to give this example and it works fine.
But I am loading the data from a CSV and here i need to set a label as Accountant only if the
csvReader.DepartmentId <> csvReader.ParentId . I tried below but it gives error. Looking for something like this. Please ignore the syntax error
*"USING PERIODIC COMMIT 1000 LOAD CSV WITH HEADERS FROM "file:///"myfile.csv AS csvReader FIELDTERMINATOR '|' * WITH csvReader WHERE NOT csvReader.EmpId IS NULL MERGE (emp:Employee{Id:toString(csvReader.EmpId)})
*ON CREATE SET emp.EmpId=csvReader.EmpId ,emp.ParentId=csvReader.ParentId, emp.DepartmentId=csvReader.DepartmentId,emp.Name=toLower(csvReader.Name),emp.LastName=csvReader.LastName, * emp:(CASE when csvReader.DepartmentId <> csvReader.ParentId then 'Accountant' ELSE END) , emp.EmpUpdated=timestamp(), emp.EmpCreated=timestamp()
*ON MATCH SET emp.EmpId=csvReader.EmpId , emp.ParentId=csvReader.ParentId, emp.DepartmentId=csvReader.DepartmentId,emp.Name=toLower(csvReader.Name),emp.LastName=csvReader.LastName, * emp:(CASE when csvReader.DepartmentId <> csvReader.ParentId then 'Accountant' ELSE END) , emp.EmpCreated=timestamp()"
USING PERIODIC COMMIT 1000 LOAD CSV WITH HEADERS FROM "file:///"myfile.csv AS csvReader FIELDTERMINATOR '|' *
WITH csvReader WHERE NOT csvReader.EmpId IS NULL
MERGE (emp:Employee{Id:toString(csvReader.EmpId)})
*ON CREATE SET emp.EmpId=csvReader.EmpId ,emp.ParentId=csvReader.ParentId, emp.DepartmentId=csvReader.DepartmentId,emp.Name=toLower(csvReader.Name),emp.LastName=csvReader.LastName,emp.EmpCreated=timestamp(), emp.EmpUpdated=timestamp() *
*ON MATCH SET emp.EmpId=csvReader.EmpId , emp.ParentId=csvReader.ParentId, emp.DepartmentId=csvReader.DepartmentId,emp.Name=toLower(csvReader.Name),emp.LastName=csvReader.LastName,emp.EmpUpdated=timestamp() *
WITH emp WHERE emp.DepartmentId <> emp.ParentId
SET emp:Accountant;
As @Thomas_Silkjaer mentioned, the FOREACH construct can be used to implement the optional portion of the label setting. This cypher implements the optional setting of the label to accountant only when the DepartmentId is not equal to the ParentId:
MERGE(n { EmpId: event.EmpId})
ON CREATE
SET n.DepartmentId=event.DepartmentId,
n.ParentId = event.ParentId
ON MATCH
SET n.DepartmentId=event.DepartmentId,
n.ParentId = event.ParentId
FOREACH (_ IN CASE WHEN NOT event.DepartmentId = event.ParentId THEN [true] ELSE [] END |
SET n:Accountant
)
RETURN n.EmpId , n.DepartmentId
This can be seen with two example sets of test data, the first where the values are equal: