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.

Duplicate check before creating node without using constraint clause

I am trying to check if a user has already created a particular node. If the user has already created it I want to return the duplicate number and if user hasnt, create the node. Using the query below:

UNWIND $testdata as row
OPTIONAL MATCH (a:Agency)
WHERE a.agency_name = row.agency_name
WITH count(a.agency_name) as cp,row
CALL apoc.do.when(
    sum(cp)=0,
    'MERGE (a:Agency {_uuid:apoc.create.uuid()})
    ON CREATE SET a+= row
    RETURN a AS data',
    'RETURN sum(cp) AS NumDuplicate',
    {row:row,cp:cp}) YIELD value
RETURN value 

However I receive an error: Procedure call cannot take an aggregating function as argument, please add a 'WITH' to your statement. I don't understand why I receive this error when I already have a WITH clause above the CALL clause.

1 ACCEPTED SOLUTION

Hello @tarendran.vivekanand

Replace sum(cp)=0, by cp=0,

Or if you want to do the SUM() of cp, you must do it in a WITH clause before the CALL.

Regards,
Cobra

View solution in original post

3 REPLIES 3

Hello @tarendran.vivekanand

Replace sum(cp)=0, by cp=0,

Or if you want to do the SUM() of cp, you must do it in a WITH clause before the CALL.

Regards,
Cobra

@Cobra Thanks for the help. Such a simple mistake my bad.