If I'm ONLY looking for a max value from a single node query, then max behaves as I expect. But as soon as I want to query some relationships, max seems to do nothing, and when I attempt to apply a LIMIT or UNWIND I end up only getting a portion of what I'm looking for.
Let me explain my query. I want to find all contracts that are still active, but are expired (their end-date is greater than today).
Then, for ALL those contracts, I'd like to find any that have had active tickets in the last year. If they have, give me the latest (LAST) date that a new ticket was created, and return all that together.
I am very close, but I seem to only be able to return EVERY ticket that is in the last year against said contracts. thus my challenge understanding aggregation with MAX().
Here's my (not quite working) Cypher:
MATCH (cc:Crmcontract) where cc.enddate < timestamp() and (cc)--(:Crmcontractstatus {value:'A'})
WITH *,apoc.date.format(coalesce(cc.enddate,0),'ms',"yyyy-MM-dd'T'HH:mm:ss",'CST') as enddate,timestamp()-31104000000 as ayearago
MATCH (cc)--(t:Ticket) where t.createdon > ayearago
WITH *,max(t.createdon) as maxticket
WITH * where t.createdon = maxticket
WITH *,apoc.date.format(maxticket,'ms',"yyyy-MM-dd'T'HH:mm:ss",'CST') as lastticket
return cc.code,cc.name,lastticket,cc.enddate