GROUP BY 可以将别名中的数据行分成多个组,各组保留一行数据,其余的行舍弃;常与聚合、排序等操作配合使用。
语法:GROUP BY <expression>
as <alias>
, <expression>
as <alias>
, ...
参数:
- <expression>:分组依据,多个依据必须同源,从左到右进行逐级分组
- <alias>:分组依据的别名,可省略
例如,对一步路径 path 进行多重分组,先按照起点 n1 的形状分组,再在组内按照终点 n1 的颜色分组;统计各组的路径数量:
n(as n1).re().n(as n2) as path
group by n1.shape, n2.color
return path, count(path)
示例图集:(以下示例将在本图基础上运行)
在空图集中依次运行以下各行代码以创建该图数据:create().node_schema("country").node_schema("movie").node_schema("director").edge_schema("filmedIn").edge_schema("direct")
create().node_property(@*, "name")
insert().into(@country).nodes([{_id:"C001", _uuid:1, name:"France"}, {_id:"C002", _uuid:2, name:"USA"}])
insert().into(@movie).nodes([{_id:"M001", _uuid:3, name:"Léon"}, {_id:"M002", _uuid:4, name:"The Terminator"}, {_id:"M003", _uuid:5, name:"Avatar"}])
insert().into(@director).nodes([{_id:"D001", _uuid:6, name:"Luc Besson"}, {_id:"D002", _uuid:7, name:"James Cameron"}])
insert().into(@filmedIn).edges([{_uuid:1, _from_uuid:3, _to_uuid:1}, {_uuid:2, _from_uuid:4, _to_uuid:1}, {_uuid:3, _from_uuid:3, _to_uuid:2}, {_uuid:4, _from_uuid:4, _to_uuid:2}, {_uuid:5, _from_uuid:5, _to_uuid:2}])
insert().into(@direct).edges([{_uuid:6, _from_uuid:6, _to_uuid:3}, {_uuid:7, _from_uuid:7, _to_uuid:4}, {_uuid:8, _from_uuid:7, _to_uuid:5}])
分组、组内聚合
示例:查找 2 步路径 国家-电影-导演,按导演分组,统计每组的路径条数
n({@country}).e().n({@movie}).e().n({@director} as n)
group by n
return table(n.name, count(n))
| n.name | count(n) |
|---------------|----------|
| Luc Besson | 2 |
| James Cameron | 3 |
分析:聚合运算如果没有紧跟着 GROUP BY 子句,则会执行分组后的聚合,而非在组内进行聚合。
多重分组
示例:查找 2 步路径 国家-电影-导演,按国家-导演分组,统计每组的路径条数
n({@country} as a).e().n({@movie}).e().n({@director} as b)
group by a, b
return table(a.name, b.name, count(a))
| a.name | b.name | count(a) |
|--------|---------------|----------|
| France | Luc Besson | 1 |
| France | James Cameron | 1 |
| USA | Luc Besson | 1 |
| USA | James Cameron | 2 |