elasticsearch 多列 聚合(sql group by)
文档数据格式
{"zone_id":"1","user_id":"100008","try_deliver_times":102,"trade_status":"TRADE_FINISHED","trade_no":"xiaomi.21142736250938334726","trade_currency":"CNY","total_fee":100,"status":"paid","sdk_user_id":"69272363","sdk":"xiaomi","price":1,"platform":"android","paid_channel":"unknown","paid_at":1427370289,"market":"unknown","location":"local","last_try_deliver_at":1427856948,"is_guest":0,"id":"fa6044d2fddb15681ea2637335f3ae6b7f8e76fef53bd805108a032cb3eb54cd","goods_name":"\u4E00\u5C0F\u5806\u5143\u5B9D","goods_id":"ID_001","goods_count":"1","expires_in":2592000,"delivered_at":0,"debug_mode":true,"created_at":1427362509,"cp_result":"exception encountered","cp_order_id":"cp.order.id.test","client_id":"9c98152c6b42c7cb3f41b53f18a0d868","app_user_id":"fvu100006"}
根据时间汇总 "client_id","platform","sdk", sum-》"total_fee"
select client_id,platform,sdk,sum(total_fee) from test group by client_id,platform,sdk
curl 'http://127.0.0.1:9200/test/orders/_search?pretty' -d '
{
"fields": ["client_id","platform","sdk","total_fee","paid_at"],
"query": {
"filtered": {
"query": { "query_string":{"query":"*"} },
"filter": {"bool": { "must": { "range": { "paid_at": {"from": 1427370289, "to": 1427948457} } } }}
}
},
"aggs": {
"clientids": { "terms": { "field": "client_id" },
"aggs": {
"sdks": { "terms": { "field": "sdk" } ,
"aggs": {
"platforms": { "terms": {"field": "platform" } ,
"aggs": {
"totalfee": { "sum": { "field": "total_fee" } }
}
}}
}
}
}
}
}'
最后结果
。。。。。
"aggregations" : {
"clientids" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "9c98152c6b42c7cb3f41b53f18a0d868",
"doc_count" : 5,
"sdks" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "xiaomi",
"doc_count" : 5,
"platforms" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "android",
"doc_count" : 5,
"totalfee" : {
"value" : 500.0
}
} ]
}
} ]
}
}, {
"key" : "999999",
"doc_count" : 3,
"sdks" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "oppo",
"doc_count" : 2,
"platforms" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "android",
"doc_count" : 2,
"totalfee" : {
"value" : 200.0
}
} ]
}
}, {
"key" : "baidu",
"doc_count" : 1,
"platforms" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "android",
"doc_count" : 1,
"totalfee" : {
"value" : 100.0
}
} ]
}
} ]
}
}, {
"key" : "888888",
"doc_count" : 2,
"sdks" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "baidu",
"doc_count" : 1,
"platforms" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "android",
"doc_count" : 1,
"totalfee" : {
"value" : 100.0
}
} ]
}
}, {
"key" : "oppo",
"doc_count" : 1,
"platforms" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "android",
"doc_count" : 1,
"totalfee" : {
"value" : 100.0
}
} ]
}
} ]
}
} ]
}
}
}
现在基本达到要求了,网上好多例子都是单列汇总 做个记录
elasticsearch 多列 聚合(sql group by)的更多相关文章
- SQL group by分组查询(转)
本文导读:在实际SQL应用中,经常需要进行分组聚合,即将查询对象按一定条件分组,然后对每一个组进行聚合分析.创建分组是通过GROUP BY子句实现的.与WHERE子句不同,GROUP BY子句用于归纳 ...
- mysql 从聚合函数group by到sql_mode
说到group by, 想必大家都不陌生, 就是对查询的数据进行分组,我们可以通过该操作实现一些特殊需求,比如去重. 最近在项目中使用HQL:" from TSjrz where CBh = ...
- SQL group by分组查询
本文导读:在实际SQL应用中,经常需要进行分组聚合,即将查询对象按一定条件分组,然后对每一个组进行聚合分析.创建分组是通过GROUP BY子句实现的.与WHERE子句不同,GROUP BY子句用于归纳 ...
- [源码解析] GroupReduce,GroupCombine 和 Flink SQL group by
[源码解析] GroupReduce,GroupCombine和Flink SQL group by 目录 [源码解析] GroupReduce,GroupCombine和Flink SQL grou ...
- 行转列:SQL SERVER PIVOT与用法解释
在数据库操作中,有些时候我们遇到需要实现“行转列”的需求,例如一下的表为某店铺的一周收入情况表: WEEK_INCOME(WEEK VARCHAR(10),INCOME DECIMAL) 我们先插入一 ...
- SQL GROUP BY 语句
合计函数 (比如 SUM) 常常需要添加 GROUP BY 语句. GROUP BY 语句 GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组. SQL GROUP BY 语法 ...
- LINQ TO SQL ——Group by
原文:LINQ TO SQL --Group by 分组在SQL中应用的十分普遍,在查询,统计时都有可能会用到它.LINQ TO SQL中同样具备group的功能,这篇我来讲下LINQ TO SQL中 ...
- SQL group 分组查询
1.使用group by进行分组查询 在使用group by关键字时,在select列表中可以指定的项目是有限制的,select语句中仅许以下几项: 被分组的列 为每个分组返回一个值得表达式,例如 ...
- 在论坛中出现的比较难的sql问题:19(row_number函数 行转列、sql语句记流水)
原文:在论坛中出现的比较难的sql问题:19(row_number函数 行转列.sql语句记流水) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记 ...
随机推荐
- 为autoLayout 增加标识符,方便调试
 如上图,是一个十分简单的布局. root view 上加了一个 button 和一个 webview. 不加标识符的样子 视图层级中没有标识  只有 UIView.WKWebView 之类,如果 ...
- day 33js 后续 函数.对象
前情提要: 今天学习的是js的函数以及简单的类的使用 一:函数的初识别 <!DOCTYPE html> <html lang="en"> <head& ...
- iOS---GCD的三种常见用法
1.一次性代码:dispatch_once 有时候,有些代码在程序中只要被执行一次. 整个程序运行过程中,只会执行一次. - (void)viewDidLoad { [super viewDidLoa ...
- centos安装 Anaconda3及使用
下载安装 下载地址https://www.anaconda.com/download/ 旧版本下载https://repo.continuum.io/archive/ 比如下载Anaconda3-4. ...
- 【LeetCode】502. IPO
题目 假设 LeetCode 即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,LeetCode希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完成最多 ...
- 【NOIP2013】转圈游戏 快速幂
题目大意:给你四个整数$n,m,k,x$,求$(x+m\times 10^k)%n$. 直接一个快速幂就好了,注意开$long\ long$. #include<bits/stdc++.h> ...
- Visual Studio 中的 Office 和 SharePoint 开发
MSDN Library 开发工具和语言 Visual Studio 中的 Office 和 SharePoint 开发 https://msdn.microsoft.com/zh-cn/libra ...
- 剑指offer三十八之二叉树的深度
一.题目 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 二.思路 递归,详见代码. 三.代码 public class So ...
- 剑指offer三十七之数字在排序数组中出现的次数
一.题目 统计一个数字在排序数组中出现的次数. 二.思路 解法一:遍历数组计数 解法二:考虑到时有序数组,所以采用分查找,找到第一个K 和 最后一个K的位置, 二者相减. 三.代码 解法一: publ ...
- hive与hbase的整合
Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供完整的sql查询功能,可以将sql语句转换为MapReduce任务进行运行.其优点学习成本低,可以通过类S ...