Search code examples
neo4jcypher

Aggregate based on different nodes with equivalent date time property


I'm trying to get an aggregating query in tabular format, based on date. The simplest way to describe my schema is:

(:A {createDate: DateTime})--(:B)--(:C {createDate: DateTime})

Right now, I was only doing aggregation on :C nodes, so aggregating was very simple:

MATCH ..
RETURN 
  date(c.createDate),
  count(c),
  avg(c.property),
  etc.

However, now that I have two independent nodes that share this property, I'm not sure how to move forward. I suppose I have to somehow unwind and collect the nodes together but I can't seem to get it to work.

EDIT:

I've discovered I can do something like this:

MATCH (a:A)--(b:B)--(c:C)
WITH CASE WHEN date(a.createDate) = date(c.createDate) THEN a ELSE {} as a
RETURN 
..

This works in practice, but it feels quite dodgy. Is that a better / recommended way to do this kind of operations?


Solution

  • Try something like this:

    MATCH (a:A)--(b:B)--(c:C)
    WITH COLLECT(a) AS aNodes, COLLECT(c) AS cNodes
    WITH aNodes + cNodes AS totalNodes
    UNWIND totalNodes AS n
    RETURN date(n.createDate), count(n), avg(n.property), etc.