# How to make a group by in cypher query language?

I have these relations:
NodeType1 A {ts:11} =r1=> NodeType2 m
NodeType1 B {ts: 22} =r1=> NodeType2 m
NodeType1 Y {ts: 22} =r1=> NodeType2 n
NodeType1 Z {ts: 32} =r1=> NodeType2 n
NodeType1 X {ts: 23} =r1=> NodeType2 o
.......

For each NodeType2, I wish to get max/min ts property NodeType1 who relate to it by r1

try use call and unwind,but not fit

1. there's no "sql group" in Neo4j
2. there's also no CQL.

The language you're using is called cypher. I suspect your asking for aggregation functions, see Aggregating functions - Cypher Manual

cypher query language => CQL?
GROUP BY is a calc method, dont care about the name

and agggregate only run on a specific node,but not unknown nodes

[quote="naughtyGitCat, post:1, topic:13725"]
why not you write parameterized query?
Could u please let me know ts is property of node or relationship?

ts => timestamp
means NodeType1 node create time

why not you write parameterized query?

you mean do forloop or foreach in program logic?

How can your node Z has two 2 create time?

I'm sorry it's a mistake

NodeType1 A {ts:11} =r1=> NodeType2 m
NodeType1 B {ts: 22} =r1=> NodeType2 m
NodeType1 Y {ts: 22} =r1=> NodeType2 n
NodeType1 Z {ts: 32} =r1=> NodeType2 n
NodeType1 X {ts: 23} =r1=> NodeType2 o

I tried below
try below:

Merge(:NodeType1 {name:'A', ts:11}) -[:r1]-> (n1:NodeType2 {name:'m'});
Merge(:NodeType1 {name:'B', ts: 22}) -[:r1]-> (n1:NodeType2 {name:'m'});
merge(:NodeType1 {name:'Y', ts: 22}) -[:r1]-> (n1:NodeType2 {name:'n'});
merge(:NodeType1 {name:'Z', ts: 32}) -[:r1]-> (n1:NodeType2 {name:'n'});
merge(:NodeType1 {name:'X', ts: 23}) -[:r1]-> (n1:NodeType2 {name:'o'});

2. Merge Nodes:
MATCH (n1:NodeType2)
WITH n1.`name` as name, collect(n1) as nodes
CALL apoc.refactor.mergeNodes(nodes, {properties: "combine"}) YIELD node
RETURN node

MATCH (n1:NodeType1)
WITH n1.`name` as name, collect(n1) as nodes
CALL apoc.refactor.mergeNodes(nodes, {properties: "combine"}) YIELD node
RETURN node

1. Aggregation Query: Example for node m
match (n1:NodeType1 ) -[r1]-> (n2:NodeType2 {name:'m'})
with n2, max(n1.ts) as maxval, min(n1.ts) as minval
return n2, maxval,minval

Yeah,

``````for NodeType2 {
// run cypher to find max(ts) NodeType1;
}
``````

is a method,

but need many times net io, and cause inconsistency

I mean do run cypher one time, to find group by type2 max(type1)

``````select max(type1) where type2.name='xxx'
``````

No offense, but I want to do this way,

``````select max(type1) group by type2.name
``````

I did not understand where did u get
select max(type1) where type2.name='xxx'

In my query as below, with n2 means you group by n2 node given in the match

match (n1:NodeType1 ) -[r1]-> (n2:NodeType2 {name:'m'})
with n2, max(n1.ts) as maxval, min(n1.ts) as minval
return n2, maxval,minval

yes, but the NodeType2 name is not specified at first, in database, there is a lot of NodeType2 node but not all named by m

As I mentioned us parameterized query. You can pass any value in parameter for an example i passed m

1. :param node2:'m'

2. match (n1:NodeType1 ) -[r1]-> (n2:NodeType2 {name:\$node2)
with n2, max(n1.ts) as maxval, min(n1.ts) as minval
return n2, maxval,minval

Yesã€‚We can send many times parameterized query to neo4j server, But this need many times net io, and cause inconsistency, unless do a explict trx.begin(), Is there any way to send a non parameterized query to finish it

Try match (n1:NodeType1 ) -[r1]-> (n2:NodeType2)
with n2, max(n1.ts) as maxval, min(n1.ts) as minval
return n2, maxval,minval

I understood what he meant, I have the same question.

I've blown a "merge" statement, so now I have too many nodes (with a specific label) to "DETACH DELETE" in one transaction. So I want to do an operation that in SQL I would accomplish with a "GROUP BY" -- I want to somehow match a group of nodes based on an attribute, "DETACH DELETE" each of those (in a single transaction), then iterate until all the nodes are gone.

I'm new to Cypher, and so I'm still struggling to understand the semantics of these group operators.

On a related note, is there any way to perform multiple transactions (inside a subquery or something) from the neobrowser (the tool that comes up on port 7474)?

MATCH (n:Label {key:value}) <- This match a group of nodes based on an attribute and a label
RETURN n