cancel
Showing results for 
Search instead for 
Did you mean: 

Duplicate check before creating node without using constraint clause

tarendran_vivek
Graph Voyager

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

Cobra
Ninja
Ninja

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

Cobra
Ninja
Ninja

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.

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