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.

Return how many specific relationship there are for a specific node

lx2pwnd
Node Clone

I have this query:

MATCH (n) 
WHERE 
	ID(n) = 770892
OPTIONAL MATCH (n)-[s:TAGGED_TOGETHER]-(d)
WHERE 
s.tagged_together >= 0 AND
((d.timestamp>'2018/01/01' AND d.timestamp<'2019/01/01') OR 
(d.removed_timestamp>'2018/01/01' AND d.removed_timestamp<'2019/01/01')) AND
d.nodeDegree>=0
RETURN n AS node, d AS tagged, size((n)-[s]-()) AS counter

I want to return how many tagged_together relationships there are for the selected node n,
but in this way the output for the column counter is always 1 for each row
although it has more of one relationship . Is there a way to return the total counter of all the relatioship or to sum each value in the column ?

1 ACCEPTED SOLUTION

The data are saved in my db in this way:

  • for all Friend nodes there is a relationship between this node and a node user
  • between some nodes Friend there is a relathionship tagged_together

At the end I have result in this way grouping by n:

MATCH (n ) 
WHERE 
	ID(n) = 770903
OPTIONAL MATCH (n)-[s:TAGGED_TOGETHER]-(d)
WHERE 
s.tagged_together >= 0 AND
((d.timestamp>'2016/01/01' AND d.timestamp<'2019/01/01') OR 
(d.removed_timestamp>'2016/01/01' AND d.removed_timestamp<'2019/01/01')) AND
d.nodeDegree>=0
RETURN n , collect(d.name) , count(s)

View solution in original post

4 REPLIES 4

intouch_vivek
Graph Steward

Hi @lx2pwnd

Please try
MATCH (n)
WHERE
ID(n) = 770892
OPTIONAL MATCH (n)-[s:TAGGED_TOGETHER]-(d)
WHERE
s.tagged_together >= 0 AND
((d.timestamp>'2018/01/01' AND d.timestamp<'2019/01/01') OR
(d.removed_timestamp>'2018/01/01' AND d.removed_timestamp<'2019/01/01')) AND
d.nodeDegree>=0
With n as node, count(s) as counter
RETURN node, counter

it seems to work, but If I want to return also the nodes d, I get 1

I do not know your model and data so unable to comment on the result
You just need to understand that when you are saying With n as node , count(s) as counter then you are doing count(s) group by n. When you need to group by d also then you need to check how many relationship are there between n and d .

The data are saved in my db in this way:

  • for all Friend nodes there is a relationship between this node and a node user
  • between some nodes Friend there is a relathionship tagged_together

At the end I have result in this way grouping by n:

MATCH (n ) 
WHERE 
	ID(n) = 770903
OPTIONAL MATCH (n)-[s:TAGGED_TOGETHER]-(d)
WHERE 
s.tagged_together >= 0 AND
((d.timestamp>'2016/01/01' AND d.timestamp<'2019/01/01') OR 
(d.removed_timestamp>'2016/01/01' AND d.removed_timestamp<'2019/01/01')) AND
d.nodeDegree>=0
RETURN n , collect(d.name) , count(s)