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.

Hello @tarendran.vivekanand :slight_smile:

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.

1 Like

No problem @tarendran.vivekanand, I was happy to help :slight_smile:

1 Like