Sonu Kapoor
Sonu Kapoor

Reputation: 1637

Multiple cypher MATCH queries to get row count

I am trying to get the row count for each node. I am using the following cypher:

MATCH (p:Product) WHERE p.modifiedDTS > 10 
RETURN {productCount: count(p)};

How can I use this with multiple unrelated matches?

MATCH (p:Product)
MATCH (i:Item)
WHERE p.modifiedDTS > 10
RETURN {productCount: count(p) ,itemCount: count(i)}

This returns 0 as the result, but should return 1 for the productCount.

Found one solution, but there are probably better options out there.

MATCH(p:Product) where p.modifiedDTS > 10 
Return count(p) as count, "product" as node
UNION ALL
MATCH(i:Item) where i.modifiedDTS > 10
Return count(i) as count, "item" as node

Upvotes: 0

Views: 99

Answers (1)

Nachshon Schwartz
Nachshon Schwartz

Reputation: 15785

Use OPTIONAL MATCH that way the where only works on the previous optional match and doesn't throw out other rows.

OPTIONAL MATCH (p:Product)
WHERE p.modifiedDTS > 10
OPTIONAL MATCH (i:Item)
WHERE i.modifiedDTS > 10
RETURN {productCount: count(p) ,itemCount: count(i)}

Upvotes: 1

Related Questions