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问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记 ...
随机推荐
- 请求数据分析 xpath语法 与lxml库
前情提要: 上节学过从网上获取请求,获取返回内容,带理 获取内容之后,第二部就是获取请求的数据分析 一:xpath 语法 浏览器一般会自带xpatn 解析 这里大概讲述一下xpath 的基本操作 二: ...
- docker私服registry管理镜像
前言 首先试想这样一个场景:当在自己的机器上(docker中)构建了mysql镜像,eureka镜像等等微服务镜像,这些镜像有可能需要放到其他的机器上docker环境中去运行,实行分布式架构部署.但如 ...
- 【BZOJ3217】ALOEXT 分块
题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3217 分块过掉辣!!!!!!$O(n^{1.5}+q\times \sqrt{n})$的 ...
- rabbitmq系列三 之发布/订阅
1.发布/订阅 在上篇教程中,我们搭建了一个工作队列,每个任务只分发给一个工作者(worker).在本篇教程中,我们要做的跟之前完全不一样 —— 分发一个消息给多个消费者(consumers).这种模 ...
- (转)windows 2003 远程桌面关闭 运行程序退出解决
windows 2003 远程桌面关闭 运行程序退出解决 原文:http://2798996.blog.51cto.com/2788996/503365 情况:远程桌面到2003,运行一个程序,然后关 ...
- JAVA 垃圾笔记一溜堆
精度只能从低精度 转到高精度.例如:float = 3.4;错误 默认小数在JAVA中是double. 即:从double高精度转到floag低精度错误!!将字符char加减乘除,默认对ASCII码运 ...
- Picasso加载网络图片失败,提示decodestream时返回null
最近遇到一个问题,项目用的图片加载框架是Picasso,网络加载框架是okhttp,项目在加载轮播图时有时可以正常加载,有时,会加载失败,提示decodestream时返回null. 首先,需要确定是 ...
- 虚拟网络编辑器的知识和出现的一些问题(没有VMnet0或VMnet8)
不多说,直接上干货! 安装好的虚拟机,没有VMnet0!!! 解决的办法: 就可以了. 那么,问题现在又来了? 但是,每次都要这样,以右键,管理员身份运行,再关闭之后,以普通双击方式,即又没有VMne ...
- docker 安装集群管理工具 docker-compose
compose将管理的容器分为3层, 工程, 服务, 容器. 一个工程中可包含多个服务, 每个服务中定义容器运行的镜像参数, 依赖, 一个服务中科包含多个容器实力, 并没有解决负载均衡的问题 dock ...
- solidity如何拼接字符串?
当你开始学习使用solidity开发以太坊智能合约之后,很快你会碰到一个问题: 一.在solidity中该如何拼接字符串? 可能你已经试过了,下面的代码试图把两个字符串使用相加的运算符连接起来,但是这 ...