文档数据格式

{"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)的更多相关文章

  1. SQL group by分组查询(转)

    本文导读:在实际SQL应用中,经常需要进行分组聚合,即将查询对象按一定条件分组,然后对每一个组进行聚合分析.创建分组是通过GROUP BY子句实现的.与WHERE子句不同,GROUP BY子句用于归纳 ...

  2. mysql 从聚合函数group by到sql_mode

    说到group by, 想必大家都不陌生, 就是对查询的数据进行分组,我们可以通过该操作实现一些特殊需求,比如去重. 最近在项目中使用HQL:" from TSjrz where CBh = ...

  3. SQL group by分组查询

    本文导读:在实际SQL应用中,经常需要进行分组聚合,即将查询对象按一定条件分组,然后对每一个组进行聚合分析.创建分组是通过GROUP BY子句实现的.与WHERE子句不同,GROUP BY子句用于归纳 ...

  4. [源码解析] GroupReduce,GroupCombine 和 Flink SQL group by

    [源码解析] GroupReduce,GroupCombine和Flink SQL group by 目录 [源码解析] GroupReduce,GroupCombine和Flink SQL grou ...

  5. 行转列:SQL SERVER PIVOT与用法解释

    在数据库操作中,有些时候我们遇到需要实现“行转列”的需求,例如一下的表为某店铺的一周收入情况表: WEEK_INCOME(WEEK VARCHAR(10),INCOME DECIMAL) 我们先插入一 ...

  6. SQL GROUP BY 语句

    合计函数 (比如 SUM) 常常需要添加 GROUP BY 语句. GROUP BY 语句 GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组. SQL GROUP BY 语法 ...

  7. LINQ TO SQL ——Group by

    原文:LINQ TO SQL --Group by 分组在SQL中应用的十分普遍,在查询,统计时都有可能会用到它.LINQ TO SQL中同样具备group的功能,这篇我来讲下LINQ TO SQL中 ...

  8. SQL group 分组查询

    1.使用group by进行分组查询  在使用group by关键字时,在select列表中可以指定的项目是有限制的,select语句中仅许以下几项:  被分组的列 为每个分组返回一个值得表达式,例如 ...

  9. 在论坛中出现的比较难的sql问题:19(row_number函数 行转列、sql语句记流水)

    原文:在论坛中出现的比较难的sql问题:19(row_number函数 行转列.sql语句记流水) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记 ...

随机推荐

  1. (原创)确保JAVA线程安全的4种常用方法

    在Java中可以有很多方法来保证线程安全,比如使用同步方法.同步块,使用原子类(atomic concurrent classes),实现并发锁,使用volatile关键字,使用不变类和线程安全类. ...

  2. Linux实用指令

    Linux实用指令 Rpm&Yum ​ 一种用于互联网下载包的打包和安装工具,它包含某些Linux分发版中,它生产具有 .rpm 扩展名的文件.RPM 是 RedHat Package Man ...

  3. RHCE 学习结构

    本文内容为本站的 blog 链接 第一章   安装初体验 第二章   访问系统 2.1 基于图形化界面访问 2.2 基于文本访问 2.3 用户管理 第三章   文件系统 3.1  Linux 文件系统 ...

  4. 使用seek()方法报错:“io.UnsupportedOperation: can't do nonzero cur-relative seeks”错误的原因

    在使用seek()函数时,有时候会报错为  “io.UnsupportedOperation: can't do nonzero cur-relative seeks”,代码如下: >>& ...

  5. Java的注解相关的命令

    与注解处理器的有关的命令有5个,分别如下: (1)-XprintProcessorInfo 输出有关请求处理程序处理哪些注释的信息 (2)-XprintRounds 输出有关注释处理循环的信息 (3) ...

  6. 【软件工程实践】第二次作业:分布式版本控制系统Git的安装与使用

    1.下载安装配置用户名和邮箱. 2. 创建工作目录并通过git init命令把这个目录变成Git可以管理的仓库. 3. 在工作目录下准备文本文件,建议下载Notepad++代替记事本. 4. 组合用g ...

  7. redis-集群创建脚本

    之前建好了redis集群, 但没有找到集群重启的机制, 停电2次, 重新创建太麻烦, 于是写了个脚本辅助启动 redis的创建过程可以看: http://www.cnblogs.com/wenbron ...

  8. 软工网络15-Alpha阶段敏捷冲刺

    一.Alpha 阶段全组总任务 二.各个成员在 Alpha 阶段认领的任务 三. 整个项目预期的任务量 四.明日各个成员的任务安排 任务 预计时长 负责人 授权界面 2h 王华俊 难度选择界面 1h ...

  9. Chrome 谷歌如何快速实现跨域

    第一步:在你的E盘或者其他盘新建一个文件夹,命名为:E:\MyChromeDevUserData 第二步:找到你的谷歌浏览器快捷图标,鼠标右键选择属性,出现以下界面: 第三步:在目标选项的最后添加:  ...

  10. JAVA-6NIO之FileChannel

    Java NIO中的FileChannel是一个连接到文件的通道.可以通过文件通道读写文件. FileChannel无法设置为非阻塞模式,它总是运行在阻塞模式下. 打开FileChannel 在使用F ...