With Clause
WITH is primarily used for further projection or aggregation of current data. Next, we will introduce related usage from these two aspects.
Aggregation
Aggregation is similar to the GROUP BY operation in SQL, which groups current data by properties and performs corresponding operations of aggregate functions on each group’s data. NeuG currently supports mainstream aggregation functions, including:
- COUNT
- COUNT_STAR
- COLLECT
- SUM
- MIN
- MAX
- AVG
We will introduce these functions in detail in the Aggregate Function Section.
Aggregate by Single Property
Match (a) With label(a) as label, count(a.name) as cnt Return label, cnt;Aggregate by Multiple Properties
Match (a)-[b:knows]->(c) With label(a) as a_label, label(c) as c_label, count(b) as cnt Return a_label, c_label, cnt;Apply Multiple Aggregate Functions
Match (a)-[b:knows]->(c) With label(a) as a_label, label(c) as c_label, count(b) as cnt, sum(b.weight) as weights Return a_label, c_label, cnt, weights;Filter based on Aggregation Results
Match (a:person) With label(a) as label, count(a.name) as cnt Where cnt > 2 Return label, cnt;Projection
Another common usage of WITH is to further project current results by columns, which is equivalent to Column Pruning in SQL, outputting only the columns needed for subsequent queries.
Project Node Data
Match (a:person {name: 'marko'})-[:knows]->(b:person)
With b
Match (b)-[:created]->(c)
Return c.name;Project Node/Edge Data
Match (a:person {name: 'marko'})-[k:knows]->(b:person)
With b, k
Match (b)-[:created]->(c)
Return k.weight, c.name;Project Properties
Match (a:person {name: 'marko'})-[:knows]->(b:person)
With a, b.age as b_age
Match (a)-[:created]->(c)
Where b_age > 20
Return c.name;Project the properties of the b data generated by the first Match, filter the properties through Filter, and finally output all c.name that meet the conditions;