聚合查询

数据聚合 groups, havings

groups 中定义数据聚合方式,Group 数据结构为 Array<Object Group>

字段类型说明必填项
groupsArray\<Object Group>聚合字段和统计层级设定
havingsArray\<Object Having>聚合查询结果筛选

Object Group 数据结构

字段类型说明必填项
fieldField Expression字段表达式,不支持设置别名 as
rollupString同时返回多层级统计结果,对应聚合字段数值的名称。
commentString聚合条件注释,用于帮助理解查询条件逻辑和在开发平台中呈现。

数据表

数据表 service :

idcitykindamount
1北京云主机50
2北京云主机50
3北京云存储50
4上海云主机200

聚合统计 group

Gou Query DSL:

function Groups() {
var query = new Query();
var data = query.Get({
select: ["type as 类型", "city as 城市", ":COUNT(id) as 数量"],
from: "service",
groups: [
{ field: "type", comment: "按类型统计" },
{ field: "city", comment: "按城市统计" },
],
});
return data;
}

解析后的 SQL语句 为(MySQL):

SELECT `type` AS `类型`, `city` AS `城市`, COUNT(`id`) AS `数量`
FROM `service`
GROUP BY `type`, `city`

查询结果为:

类型城市数量
1北京3

聚合函数 aggregate functions

聚合函数按特定算法,对一组数据记录统计,返回一个数值,一般用于聚合查询。

数据表

idcitykindamount
1北京云主机50
2北京云主机50
3北京云存储50
4上海云主机200

聚合函数表

函数参数表说明SELECT 示例数值
SUM()[Field Expression]求和:SUM(amount) , :SUM(DISTINCT amount)350, 250
COUNT()[Field Expression]记录行数:COUNT(id),:COUNT(amount), :COUNT(DISTINCT amount)4,4, 2
AVG()[Field Expression]平均数:AVG(amount), :AVG(DISTINCT amount)87.5000, 125.0000
MIN()[Field Expression]最小值:MIN(amount)50
MAX()[Field Expression]最大值:MAX(amount)200
STDDEV_POP()[Field Expression]总体标准差:STDDEV_POP(amount)64.9519052838329
STDDEV_SAMP()[Field Expression]样品标准差:STDDEV_SAMP(amount)75
VAR_POP()[Field Expression]总体方差:VAR_POP(amount)4218.75
VAR_SAMP()[Field Expression]样品方差:VAR_SAMP(amount)5625
JSON_ARRAYAGG()[Field Expression]合并为一个 JSON Array:JSON_ARRAYAGG(amount)[50, 50, 50, 200]
JSON_OBJECTAGG()[Field Expression,Field Expression]合并为一个 JSON Object:JSON_OBJECTAGG(city,price){"上海": 200, "北京": 50}

数据聚合语法糖 groups syntactic sugar

Groups 简写

在通常情况下,只需指定聚合字段即可,可使用 Array<Field Expression> 结构描述; 亦可使用 Field Expression 类型描述,多个用 , 分割。

以下描述解析结果相同。

完整描述

{
"select": ["kind as 类型", "city as 城市", ":SUM(amount) as 数量"],
"from": "service",
"groups": [
{ "field": "kind", "comment": "按类型统计" },
{ "field": "city", "comment": "按城市统计" }
]
}

简化为 Array<Field Expression>

{
"select": ["kind as 类型", "city as 城市", ":SUM(amount) as 数量"],
"from": "service",
"groups": ["kind", { "field": "city", "comment": "按城市统计" }]
}

简化为 Field Expression

{
"select": ["kind as 类型", "city as 城市", ":SUM(amount) as 数量"],
"from": "service",
"groups": "kind,city"
}

JSON Array 字段聚合

在对 JSON Array 字段聚合查询时,需要先连接 JSON Table,再对连接后的数据表进行聚合查询。可以简写为直接按 JSON Array 字段聚合。

数据表 service :

idcitykindindustries(JSON Array)pricing(JSON Array\<Object>)amount
1北京云主机["教育","医疗"][{"id":1, "name":"按月付费"},{"id":2, "name":"按年付费"}]50
2北京云主机["艺术","医疗"][{"id":1, "name":"按月付费"},{"id":3, "name":"一次性付费"}]50
3北京云存储["教育","制造"][{"id":2, "name":"按年付费"},{"id":3, "name":"一次性付费"}]50
4上海云主机["能源","汽车"][{"id":1, "name":"按月付费"}]200

Query DSL:

{
"select": [
"@industries as 行业",
"@pricing[*].name as 计费方式",
":SUM(amount) as 数量"
],
"from": "service",
"groups": [
{ "field": "行业", "rollup": "全部行业" },
{ "field": "计费方式", "rollup": "所有计费方式" }
]
}

解析后的 SQL语句 (MySQL8):

SELECT
IF(GROUPING(`行业`), '全部行业', `行业`) AS `行业`,
IF(GROUPING(`计费方式`), '所有计费方式', `计费方式`) AS `计费方式`,
SUM(`amount`) AS `数量`
FROM `service`
JOIN JSON_TABLE(`service`.`industries`, '$[*]' columns (`行业` varchar(50) path '$') ) as industries
JOIN JSON_TABLE(`service`.`pricing`, '$[*]' columns (`计费方式` varchar(50) path '$.name') ) as pricing
GROUP by `行业`, `计费方式` WITH ROLLUP

返回结果:

行业计费方式数量
制造一次性付费50
制造按年付费50
制造所有计费方式100
医疗一次性付费50
医疗按年付费50
医疗按月付费100
医疗所有计费方式200
教育一次性付费50
教育按年付费100
教育按月付费50
教育所有计费方式200
汽车按月付费200
汽车所有计费方式200
能源按月付费200
能源所有计费方式200
艺术一次性付费50
艺术按月付费50
艺术所有计费方式100
全部行业所有计费方式1000

聚合查询

数据聚合 groups, havings

groups 中定义数据聚合方式,Group 数据结构为 Array<Object Group>

字段类型说明必填项
groupsArray\<Object Group>聚合字段和统计层级设定
havingsArray\<Object Having>聚合查询结果筛选

Object Group 数据结构

字段类型说明必填项
fieldField Expression字段表达式,不支持设置别名 as
rollupString同时返回多层级统计结果,对应聚合字段数值的名称。
commentString聚合条件注释,用于帮助理解查询条件逻辑和在开发平台中呈现。

数据表

数据表 service :

idcitykindamount
1北京云主机50
2北京云主机50
3北京云存储50
4上海云主机200

聚合统计 group

Gou Query DSL:

function Groups() {
var query = new Query();
var data = query.Get({
select: ["type as 类型", "city as 城市", ":COUNT(id) as 数量"],
from: "service",
groups: [
{ field: "type", comment: "按类型统计" },
{ field: "city", comment: "按城市统计" },
],
});
return data;
}

解析后的 SQL语句 为(MySQL):

SELECT `type` AS `类型`, `city` AS `城市`, COUNT(`id`) AS `数量`
FROM `service`
GROUP BY `type`, `city`

查询结果为:

类型城市数量
1北京3

聚合函数 aggregate functions

聚合函数按特定算法,对一组数据记录统计,返回一个数值,一般用于聚合查询。

数据表

idcitykindamount
1北京云主机50
2北京云主机50
3北京云存储50
4上海云主机200

聚合函数表

函数参数表说明SELECT 示例数值
SUM()[Field Expression]求和:SUM(amount) , :SUM(DISTINCT amount)350, 250
COUNT()[Field Expression]记录行数:COUNT(id),:COUNT(amount), :COUNT(DISTINCT amount)4,4, 2
AVG()[Field Expression]平均数:AVG(amount), :AVG(DISTINCT amount)87.5000, 125.0000
MIN()[Field Expression]最小值:MIN(amount)50
MAX()[Field Expression]最大值:MAX(amount)200
STDDEV_POP()[Field Expression]总体标准差:STDDEV_POP(amount)64.9519052838329
STDDEV_SAMP()[Field Expression]样品标准差:STDDEV_SAMP(amount)75
VAR_POP()[Field Expression]总体方差:VAR_POP(amount)4218.75
VAR_SAMP()[Field Expression]样品方差:VAR_SAMP(amount)5625
JSON_ARRAYAGG()[Field Expression]合并为一个 JSON Array:JSON_ARRAYAGG(amount)[50, 50, 50, 200]
JSON_OBJECTAGG()[Field Expression,Field Expression]合并为一个 JSON Object:JSON_OBJECTAGG(city,price){"上海": 200, "北京": 50}

数据聚合语法糖 groups syntactic sugar

Groups 简写

在通常情况下,只需指定聚合字段即可,可使用 Array<Field Expression> 结构描述; 亦可使用 Field Expression 类型描述,多个用 , 分割。

以下描述解析结果相同。

完整描述

{
"select": ["kind as 类型", "city as 城市", ":SUM(amount) as 数量"],
"from": "service",
"groups": [
{ "field": "kind", "comment": "按类型统计" },
{ "field": "city", "comment": "按城市统计" }
]
}

简化为 Array<Field Expression>

{
"select": ["kind as 类型", "city as 城市", ":SUM(amount) as 数量"],
"from": "service",
"groups": ["kind", { "field": "city", "comment": "按城市统计" }]
}

简化为 Field Expression

{
"select": ["kind as 类型", "city as 城市", ":SUM(amount) as 数量"],
"from": "service",
"groups": "kind,city"
}

JSON Array 字段聚合

在对 JSON Array 字段聚合查询时,需要先连接 JSON Table,再对连接后的数据表进行聚合查询。可以简写为直接按 JSON Array 字段聚合。

数据表 service :

idcitykindindustries(JSON Array)pricing(JSON Array\<Object>)amount
1北京云主机["教育","医疗"][{"id":1, "name":"按月付费"},{"id":2, "name":"按年付费"}]50
2北京云主机["艺术","医疗"][{"id":1, "name":"按月付费"},{"id":3, "name":"一次性付费"}]50
3北京云存储["教育","制造"][{"id":2, "name":"按年付费"},{"id":3, "name":"一次性付费"}]50
4上海云主机["能源","汽车"][{"id":1, "name":"按月付费"}]200

Query DSL:

{
"select": [
"@industries as 行业",
"@pricing[*].name as 计费方式",
":SUM(amount) as 数量"
],
"from": "service",
"groups": [
{ "field": "行业", "rollup": "全部行业" },
{ "field": "计费方式", "rollup": "所有计费方式" }
]
}

解析后的 SQL语句 (MySQL8):

SELECT
IF(GROUPING(`行业`), '全部行业', `行业`) AS `行业`,
IF(GROUPING(`计费方式`), '所有计费方式', `计费方式`) AS `计费方式`,
SUM(`amount`) AS `数量`
FROM `service`
JOIN JSON_TABLE(`service`.`industries`, '$[*]' columns (`行业` varchar(50) path '$') ) as industries
JOIN JSON_TABLE(`service`.`pricing`, '$[*]' columns (`计费方式` varchar(50) path '$.name') ) as pricing
GROUP by `行业`, `计费方式` WITH ROLLUP

返回结果:

行业计费方式数量
制造一次性付费50
制造按年付费50
制造所有计费方式100
医疗一次性付费50
医疗按年付费50
医疗按月付费100
医疗所有计费方式200
教育一次性付费50
教育按年付费100
教育按月付费50
教育所有计费方式200
汽车按月付费200
汽车所有计费方式200
能源按月付费200
能源所有计费方式200
艺术一次性付费50
艺术按月付费50
艺术所有计费方式100
全部行业所有计费方式1000