0
votes

I am confused about how collect works when I unwind it.

I am collecting node properties from various optional matches and ending up with list that has duplicates:

list=[
  [ID: 1, value: 0],
  [ID: 1, value: 4],
  [ID: 1, value: 90],
  [ID: 2, value: 40],
  [ID: 2, value: 0],
  ...,
]

I can't collect just the nodes - it has to be a list because the values are computed as I collect them.

UNWIND list AS node
WITH distinct node

Unwind above does not bring it down to just one row for ID: 1, which I understand because at this point the list items are different and not related to the ID: 1 node anymore.

I'd like to only keep the row with the highest value for each ID. Can this be done on the RETURN? Expected result: [[ID: 1, value: 90], [ID: 2, value: 40]]

I looked at apoc list and map functions but nothing seems to address this. Is there a reduce or coalesce type thing that would work?

TIA!

1

1 Answers

1
votes

I presume that your lists contain maps like {ID: 1, value: 0}, not illegal constructs like [ID: 1, value: 0].

You can use the aggregating function MAX:

UNWIND list AS node
RETURN {ID: node.ID, value: MAX(node.value)};

Or, if you want the result to be a list of maps:

UNWIND list AS AS node
WITH {ID: node.ID, value: MAX(node.value)} AS item
RETURN COLLECT(item);