I think there are two ways doing this, one is yours which I'd change a bit.

MATCH (user:User)-[rel:LOVES]->(something:Something)
WITH user, something, MAX(rel.timestamp) AS timesort
ORDER BY sid.additionalSortCriteria, timesort
MATCH (user)-[rel:LOVES {timestamp : timesort}]->(something)
RETURN user,rel,something;

Alternatively you can do:

MATCH (user:User)-[rel:LOVES]->(something:Something)
WITH user, something, rel
ORDER BY sid.additionalSortCriteria, rel.timestamp
RETURN user, something, collect(rel)[0] as rel



On Thu, Aug 7, 2014 at 11:03 AM, Manuel Zamora-Morschhäuser <
[email protected]> wrote:

> The result set looks like this:
>
> (user {uid: 2})-[:LOVES {timestamp: "2013-02-01"}]-(something {sid:
> 1, additionalSortCriteria: 1})
> (user {uid: 6})-[:LOVES {timestamp: "2013-02-04"}]-(something {sid:
> 6, additionalSortCriteria: 4})
> (user {uid: 8})-[:LOVES {timestamp: "2013-01-05"}]-(something {sid:
> 10, additionalSortCriteria: 10})
> ...
>
> Every Something is returned only once (distinct), sorted by
> the additionalSortCriteria, and for every matching combination the newest
> relation is returned.
>
> Having skimmed the articles I don't think time-based versioning applies to
> my problem, but I will give them a proper read later.
>
> Thank you,
> Manuel
>
>
> Am Donnerstag, 7. August 2014 04:48:51 UTC+2 schrieb Kenny. Bastani:
>
>> I suggest taking a look at time-based versioning of graphs.
>>
>> http://www.neo4j.org/graphgist?608bf0701e3306a23e77
>>
>> Also, a thorough blog post on the subject by Ian Robinson, author of the
>> Graph databases book: http://iansrobinson.com/2014/
>> 05/13/time-based-versioned-graphs/
>>
>> Can you provide an example of your results? In your example your
>> timestamp is a string. They should be an integer representation of your
>> datetime, something like unix timestamp milliseconds since epoch. You can
>> also store the date in its long form as a string just for reference.
>>
>> Thanks,
>>
>> Kenny
>>
>> On Wednesday, August 6, 2014 9:36:43 AM UTC-7, Manuel Zamora-Morschhäuser
>> wrote:
>>>
>>> Hello everyone,
>>>
>>> I'm currently designing a cypher query which uses DISTINCT in
>>> combination with aggregations and sorting and I'm not quite sure if I'm
>>> doing this the right way.
>>>
>>> My schema looks like this:
>>>
>>> (user {userId: ..})-[rel:LOVES|HATES|IGNORES|... {timestamp:
>>> ..}]->(something {somethingId: .., additionalSortCriteria: ...})
>>>
>>> I need a distinct list of Somethings, with only the newest relation of
>>> any type, sorted by the additionalSortCriteria of Something.
>>>
>>> I have for example:
>>>
>>> (user {uid: 1})-[:LOVES {timestamp: "2013-01-01"}]->(something {sid:
>>> 1, additionalSortCriteria: 1})
>>> (user {uid: 2})-[:LOVES {timestamp: "2013-02-01"}]->(something {sid:
>>> 1, additionalSortCriteria: 1})
>>> (user {uid: 1})-[:LOVES {timestamp: "2013-03-01"}]->(something {sid:
>>> 2, additionalSortCriteria: 2})
>>> ...
>>>
>>> I use this query:
>>>
>>> MATCH (user:User)-[rel:LOVES]->(something:Something)
>>> WITH DISTINCT(something.sid) as sid, MAX(rel.timestamp) AS timesort,
>>> MAX(something.additionalSortCriteria) AS somethingsort ORDER BY
>>> somethingsort, timesort
>>> MATCH (user:User)-[rel:LOVES]->(something:Something) WHERE
>>> something.sid=sid AND rel.timestamp=timesort AND something.
>>> additionalSortCriteria=somethingsort
>>> RETURN user,rel,something;
>>>
>>> And get a list of user, relations, somethings with only distinct
>>> somethings, sorted by the somethings.additionalSortCriteria criteria
>>> and only the newest relation... it seems so at least.
>>>
>>> The individual combinations of something.sid and relation.timestamps are
>>> mostly unique, especially in combination with the additionalSortCriteria.
>>>
>>> Did I get it right or have I overlooked something? Is there a better way
>>> to do this?
>>>
>>>
>>> Thank you!
>>>
>>> Manuel
>>>
>>>
>>>  --
> You received this message because you are subscribed to the Google Groups
> "Neo4j" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to