基于聚合查询(二)求出每个颜色的销售数量、平均价格、最大价格、最小价格、价格总和
查询起来比较麻烦:
GET /tvs/_search
{
"size": 0,
"aggs": {
"group_color": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
},
"max_price": {
"max": {
"field": "price"
}
},
"min_price": {
"min": {
"field": "price"
}
},
"sum_price": {
"sum": {
"field": "price"
}
}
}
}
}
}
想要通过sql的方式去查询出结果:
操作如下:
GET /_sql?format=txt
{
"query": """
SELECT color, avg(price), sum(price), min(price), max(price) FROM "tvs" group by color
"""
}
返回结果: