Anshul
Anshul

Reputation: 211

Cypher Query - Returning weight of each matching relationship with total weight

I am trying to find percentage in the following example

START n=node:name_idx(NAME="ABC")
match p = n-[r1:LIKES]->m
with r1.Frequency as Frequency, Sum(r1.Frequency) as Sum
return Frequency, Sum

I was hoping to get something like this

Frequency        Sum
12               19
6                19
1                19

and so forth.

What I get is same value in Frequency and Sum columns

 Frequency        Sum
    12               12
    6                6
    1                1

Any suggestions how to get correct distribution? My ultimate goal is to find out the percentage by dividing Frequency/Sum of reach returning row. Thanks

Upvotes: 2

Views: 1115

Answers (2)

Eve Freeman
Eve Freeman

Reputation: 33155

How about this one (I stole Luanne's console graph):

http://console.neo4j.org/r/3axtkq

START n=node:node_auto_index(name="a") 
MATCH n-[r1:LIKES]->m 
WITH sum(r1.frequency) AS total, n // we want the total... of all frequencies for n
MATCH n-[r1:LIKES]->m              // we need to get the likes again, because those can't be passed in with and get the total of all at the same time
RETURN r1.frequency, total, r1.frequency/(total*1.0) // it does integer math unless you force one to be a float

Upvotes: 6

Luanne
Luanne

Reputation: 19373

Take a look at http://console.neo4j.org/r/voavd2

The data is:

 START n=node(1)
 MATCH n-[r1:LIKES]->m
 RETURN r1,m

Your query returns:

 START n=node(1)
 MATCH n-[r1:LIKES]->m 
 WITH r1.frequency AS Frequency, Sum(r1.frequency) AS Sum 
 RETURN Frequency, Sum

i.e. grouped by frequency, what is the sum of all frequencies for that value.

Is that what you were trying to get?

Upvotes: 2

Related Questions