# Combining queries

**URL:** <https://community.neo4j.com/t/combining-queries/13258>\
**Category:** Cypher\
**Created:** [January 2, 2020, 2:31am UTC](https://community.neo4j.com/t/combining-queries/13258 "2020-01-02T02:31:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![leelandclay](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/leelandclay/32/3954_2.png) [@leelandclay](https://community.neo4j.com/u/leelandclay)\
**Post date:** [January 2, 2020, 2:31am UTC](https://community.neo4j.com/t/combining-queries/13258/1 "2020-01-02T02:31:27Z")

</div>

Currently, I perform these updates with multiple calls that return success or failure and use that result to determine whether to continue. In order to try and reduce the number of queries I'm trying to see if it's possible to do this update with a single call.

Here's the background. I have an API Endpoint that receives calls from Patreon for new and updated pledges. However, the email that the datapacket from Patreon sends may or may not match the email I have in the database. There's 4 different possibilities that I handle.

1. Person node's email property matches the patreon information, but there is no relationship
2. Person node's email property does not match the patreon information, but the IS\_A\_SUPPORTER relationship exists with the email address
3. Person node's email property does not match the patreon information and there is no existing relationship with that email address, however there's an existing Pledge node with the email
4. Person node's email property does not match the patreon information and there is no existing relationship with that email address and no existing Pledge node with the email

The actions for each are:

1. Create a IS\_A\_SUPPORTER relationship and set the properties with items from the patreon information
2. Update the relationship properties with items from the patreon information
3. Update the Pledge node that matches the email from patreon
4. Create a Pledge node that holds the patreon information.

A user can then "claim" it by entering that email into a form where I query and create the Relationship. When the relationship is created, I delete the Pledge node.

Like I said, individually, these are very simple. I'm using the .Net Driver and simply create a session then call Run for the first query...if no results, run second...etc until final is to create the Pledge node.

My question is...can this be done with a single query? I've been trying to get it using CASE statements, but whenever I try to perform any operations I get an unexpected character error.

```auto
WITH 'lclay@kaolinsoftware.com' as patreonEmail
OPTIONAL MATCH (p:Person)-[r:IS_A_SUPPORTER {email: patreonEmail}]->(:Donation)
OPTIONAL MATCH (o:Person {email: patreonEmail})
OPTIONAL MATCH (m:Pledge {email: patreonEmail})
RETURN p, r, o, m,
CASE 
WHEN p IS NOT NULL THEN 
SET r.pledge_amount_cents = 100000 
WHEN o IS NOT NULL THEN 
'o not null' 
WHEN m IS NOT NULL THEN 
'm not null' 
END AS results

```

---

<div class="post-metadata">

**Author:** ![mojo2go](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/mojo2go/32/6363_2.png) [@mojo2go](https://community.neo4j.com/u/mojo2go)\
**Post date:** [January 3, 2020, 3:04pm UTC](https://community.neo4j.com/t/combining-queries/13258/2 "2020-01-03T15:04:21Z")

</div>

> [@leelandclay](#):
>
> SET r.pledge\_amount\_cents = 100000

The first thing I see is that SET is not supported in the CASE statement. The CASE statement is meant to evaluate a series of boolean expressions and return the first one that evaluates to true, and then returns the specified value or expression that evaluates to a value.

---

<div class="post-metadata">

**Author:** ![leelandclay](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/leelandclay/32/3954_2.png) [@leelandclay](https://community.neo4j.com/u/leelandclay)\
**Post date:** [January 3, 2020, 10:27pm UTC](https://community.neo4j.com/t/combining-queries/13258/3 "2020-01-03T22:27:37Z")

</div>

That's the idea I was getting, but wanted to make sure I wasn't misunderstanding it. I think I'm going to have to leave it as it is. It's not a big deal, but I just don't like seeing multiple if statements in a row :)

---

<div class="post-metadata">

**Author:** ![mojo2go](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/mojo2go/32/6363_2.png) [@mojo2go](https://community.neo4j.com/u/mojo2go)\
**Post date:** [January 6, 2020, 9:16am UTC](https://community.neo4j.com/t/combining-queries/13258/4 "2020-01-06T09:16:06Z")

</div>

Okay, so you do have working code but have not posted it here.  
Just a helpful comment. I find that when I post a question here, I get responses faster if I can provide code to build a tiny dataset to use for showing my problem, along with my code (working or not). It becomes a fun puzzle for someone else to solve for you.

---

<div class="post-metadata">

**Author:** ![ganesanmithun323](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ganesanmithun323/32/5414_2.png) [@ganesanmithun323](https://community.neo4j.com/u/ganesanmithun323)\
**Post date:** [January 6, 2020, 11:13am UTC](https://community.neo4j.com/t/combining-queries/13258/5 "2020-01-06T11:13:57Z")

</div>

@leelandclay [Neo4j APOC Procedures User Guide](http://neo4j-contrib.github.io/neo4j-apoc-procedures/3.5/cypher-execution/conditionals/)

please have a look at this..i think this can help you

---

<div class="post-metadata">

**Author:** ![leelandclay](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/leelandclay/32/3954_2.png) [@leelandclay](https://community.neo4j.com/u/leelandclay)\
**Post date:** [January 9, 2020, 6:38pm UTC](https://community.neo4j.com/t/combining-queries/13258/6 "2020-01-09T18:38:19Z")

</div>

@ganesanmithun323 That looks like exactly what I'm looking for. I've already moved onto another story at the moment so it'll be a while before I can test this, but I will 🙂  
Thanks!!!!!
