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.

Reply via email to