# Compare (count) Relationships to a Single Node

**URL:** <https://community.neo4j.com/t/compare-count-relationships-to-a-single-node/17541>\
**Category:** Cypher\
**Tags:** apoc, cypher, operations, knowledge-base\
**Created:** [April 23, 2020, 8:24pm UTC](https://community.neo4j.com/t/compare-count-relationships-to-a-single-node/17541 "2020-04-23T20:24:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthew.cash](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/matthew.cash/32/3913_2.png) [@matthew.cash](https://community.neo4j.com/u/matthew.cash)\
**Post date:** [April 23, 2020, 8:24pm UTC](https://community.neo4j.com/t/compare-count-relationships-to-a-single-node/17541/1 "2020-04-23T20:24:12Z")

</div>

I am trying to count the differences in relationships pointing to a certain node.. If I write the code below with **rel** = 1 and **ler** = 1 then I get a proper result showing the count of both having 1 of each type of a relationship.. but I want to also know when there is 1 of **rel** and 0 of **ler**  
When I try to run this I get 0 results, but know this is not true..

```auto
MATCH ()-[rs:OWNED_BY {type:'ipAddress',source:'attr'}]-(s:SourceValue)-[sr:OWNED_BY {type:'ipAddress',source:'ca_model'}]-()
WITH s, count(rs) as rel, count(sr) as ler
WHERE rel = 1 and ler = 0
RETURN count(s)

MATCH ()-[rs:OWNED_BY {type:'ipAddress',source:'attr'}]-(s:SourceValue)-[sr:OWNED_BY {type:'ipAddress',source:'ca_model'}]-()
WITH s, count(rs) as rel, count(sr) as ler
WHERE rel > 1 and ler = 1
RETURN count(s)

```

---

<div class="post-metadata">

**Author:** ![anthapu](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/anthapu/32/4113_2.png) [@anthapu](https://community.neo4j.com/u/anthapu)\
**Post date:** [April 23, 2020, 8:33pm UTC](https://community.neo4j.com/t/compare-count-relationships-to-a-single-node/17541/2 "2020-04-23T20:33:19Z")

</div>

May be you can use union to get both in a single query

```auto
MATCH ()-[rs:OWNED_BY {type:'ipAddress',source:'attr'}]-(s:SourceValue)-[sr:OWNED_BY {type:'ipAddress',source:'ca_model'}]-()
WITH s, count(rs) as rel, count(sr) as ler
WHERE rel = 1 and ler = 0
RETURN count(s) as count, "no_ler" as status
UNION 
MATCH ()-[rs:OWNED_BY {type:'ipAddress',source:'attr'}]-(s:SourceValue)-[sr:OWNED_BY {type:'ipAddress',source:'ca_model'}]-()
WITH s, count(rs) as rel, count(sr) as ler
WHERE rel > 1 and ler = 1
RETURN count(s) as count, "with_ler" as status

```

This way you can get both counts and still have the context for the counts.

---

<div class="post-metadata">

**Author:** ![matthew.cash](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/matthew.cash/32/3913_2.png) [@matthew.cash](https://community.neo4j.com/u/matthew.cash)\
**Post date:** [April 23, 2020, 8:50pm UTC](https://community.neo4j.com/t/compare-count-relationships-to-a-single-node/17541/3 "2020-04-23T20:50:22Z")

</div>

> [@anthapu](#):
>
> MATCH ()-[rs:OWNED\_BY {type:'ipAddress',source:'attr'}]-(s:SourceValue)-[sr:OWNED\_BY {type:'ipAddress',source:'ca\_model'}]-() WITH s, count(rs) as rel, count(sr) as ler WHERE rel = 1 and ler = 0 RETURN count(s) as count, "no\_ler" as status

In my original post, those are actually two different queries I am trying to get results from, not two I am trying to combine, but neither of them work as they are currently set up.

---

<div class="post-metadata">

**Author:** ![andrew\_bowman](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/andrew_bowman/32/73_2.png) [@andrew\_bowman](https://community.neo4j.com/u/andrew_bowman)\
**Post date:** [April 23, 2020, 10:16pm UTC](https://community.neo4j.com/t/compare-count-relationships-to-a-single-node/17541/4 "2020-04-23T22:16:23Z")

</div>

Use the size() function with the pattern that you're looking for, that avoids cardinality issues and the need for aggregations:

```auto
MATCH (s:SourceValue)
WITH s, size(()-[:OWNED_BY {type:'ipAddress',source:'attr'}]-(s)) as rel, size((s)-[:OWNED_BY {type:'ipAddress',source:'ca_model'}]-()) as ler
WHERE rel = 1 and ler = 0
RETURN count(s)

```

---

<div class="post-metadata">

**Author:** ![matthew.cash](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/matthew.cash/32/3913_2.png) [@matthew.cash](https://community.neo4j.com/u/matthew.cash)\
**Post date:** [April 24, 2020, 4:11pm UTC](https://community.neo4j.com/t/compare-count-relationships-to-a-single-node/17541/5 "2020-04-24T16:11:13Z")

</div>

> [@andrew\_bowman](#):
>
> MATCH (s:SourceValue) WITH s, size(()-[:OWNED\_BY {type:'ipAddress',source:'attr'}]-(s)) as rel, size((s)-[:OWNED\_BY {type:'ipAddress',source:'ca\_model'}]-()) as ler WHERE rel = 1 and ler = 0 RETURN count(s)

sheer genius, thank you for resolving my headache!
