文档数据格式

{"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. 常见手机的useragent

    设备 系统 浏览器 User-Agent 华为Mate 10 Pro Android QQ浏览器 Mozilla/5.0 (Linux; U; Android 8.1.0; zh-cn; BLA-AL ...

  2. WebDriverAPI(3)

    获取页面的Title属性 被测网址http:http://www.baidu.com Java语言版本的API实例代码 String url = "http://www.baidu.com& ...

  3. Numpy 数组和dtype的一个使用误区

    首先自定义三种类型(如下代码1-3行),第一行使用scalar type,第2,3行使用Structured type. 提出问题:第5,7行同为创建数组,为什么第5行能work,而第7行会raise ...

  4. (转)rootvg镜像

    步骤1:查看当前还未加入到其它vg的可用PV  # lspv  hdisk0          00027c6a0507fe17                    rootvg          ...

  5. warning: already initialized constant FileUtils::VERSION

    Ran into this, and the solution here works: https://stackoverflow.com/questions/51334732/rails-5-2-0 ...

  6. 回退Ubuntu记录

    前言 由于Ubuntu18经常出错,因而决定回退Ubuntu16,下面是记录回退问题及美化,以便以后需要. 问题总结 磁盘挂载 挂载其他磁盘分区时,提示错误"Metadata kept in ...

  7. 【Express系列】第1篇——项目创建

    安装 node 和 Express 4 node官网:http://nodejs.org/ Express Github:https://github.com/expressjs/express   ...

  8. Python虚拟环境中pip install时没有权限问题

    virtualenv Permission denied 新建的python虚拟环境的目录的所属者必须是当前用户,才不会出现这种错误 比如 virtualenv py27 sudo chown zzf ...

  9. springcloud-03-服务注册

    新建一个 provider-user 和consumer-movie, user为服务提供者, movie为服务的消费真, 没有什么难的, 直接上代码 microserver-provider-use ...

  10. Java线程池,你了解多少?

    一.前言 随着业务的发展,单线程已经远远不能满足,随即就有多线程的出现.多线程虽然能解决单线程解决不了的事情,但是它也会给你带来额外的问题.比如成千上万甚至上百万的线程时候,你系统就会出现响应延迟.卡 ...