Jon M
Jon M

Reputation: 43

Cypher: Can't get grouped node counts from connected nodes

Apologies for the likelihood that this is a repeat question, but I perused questions already asked and either couldn't find one that related to my issue or couldn't figure out HOW they related to my issue. I'm also a cypher neophyte so this is also likely a very simple question.

I'm trying to determine the name of each container, the total number of containers, and the number of items in each container so that I can generate a pie graph representing the distribution of items amongst containers. The queries I've constructed so far give me a true count of the items, but either give me a count of 1 for the containers (such as the query below) or return the container count with the same value as the items count.

START c=node:internal_auto_index( TYPE = 'CONTAINERS' ) 
WITH c, COUNT( c.name ) as containerCount
MATCH i-[INSTANCE__CONTAINERS]->c 
RETURN c.name as containerName, containerCount, COUNT(i) as items, ( COUNT(i) / containerCount ) as percentage
ORDER BY c.name

What is it I'm doing wrong? Thanks in advance for your time.

Cheers,

Jon

Upvotes: 3

Views: 173

Answers (1)

Lisa Li
Lisa Li

Reputation: 2592

The result of the first query has two expressions. The first expression c serves as the key of the group to which the aggregation function count() of the second expression is applied. Since each c represents a group of a single container, the aggregation function count would certainly be evaluated to 1 rather than the total number of containers.

The easy way to fix this is to just return the count of the container in the first query and carried it forward to the second query,

START c=node:internal_auto_index( TYPE = 'CONTAINERS' ) 
WITH  COUNT(*) as containerCount
START c=node:internal_auto_index( TYPE = 'CONTAINERS' ) 
MATCH i-[INSTANCE__CONTAINERS]->c 
RETURN c.name as containerName, containerCount, COUNT(i) as items, ( COUNT(i) / containerCount ) as percentage
ORDER BY c.name

I know it looks a bit redundant, but I don't think there is a way to return each container and the total number of container in a single row as a query result.

Upvotes: 3

Related Questions