cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Help with top 10 values in cypher query

I am trying to get the top 10 values of a property P1 in node N1 for each property P2 in the same node N1. So far I have tried the following:

match (n:N1)
where n.P2 is not null
with 
n.P2 as test, count(*) as times 
with collect ({P2:test,times:times}) as rows, count(*) as times1
order by times1 desc
unwind [row in rows] as row
return row.P2 as test, row.times as times
order by times desc
limit 10

I'm not sure if this is giving me the right values.
Another thing I tried is as follows:

match (m:N1)
where m.P1 is not null

with m
match(n:N1{P1:m.P1})
where n.P2 is not null
return 
m.P1 as test1, n.P2 as test2, count(*) as domainCount
order by domainCount desc
limit 10

I'm trying to run this on the Neo4j browser version 1.4.7. Properties P1 and P2 are both Strings. There can be duplicate values of property P2 for each property P1.
I'm not sure if I am on the right track to solving this. Any help is appreciated.

2 REPLIES 2

Hi,

Please try this to see if it solves your problem->

MATCH (n:N1)
WITH DISTINCT n.P2 AS p2Value
CALL {
WITH p2Value
MATCH (n:N1{P2:p2Value})
RETURN p2Value, n.P1 as p1Value, count(*) AS times LIMIT 10
}
RETURN p2Value, p1Value, times;

Regards,
D

ameyasoft
Graph Maven
Try this:

match (n:N1)
with distinct n.P2 as p2, count(distinct n.P1) as p1cnt
return p2, p1cnt order by p1cnt desc limit 10

//to get n.P1 values....

match (n:N1)
with distinct n.P2 as p2, collect(distinct n.P1) as p1
return p2, p1, size(p1) as p1cnt order by p1cnt desc limit 10