rei*_*neh 6 sql oracle dsl aggregation elasticsearch
我想将此SQL查询转换为弹性DSL查询语言
SELECT t.pk_c_c_s,
t.fk_c_c_id,
t.s_b_a,
t.datetime,
SUBSTR(t.datetime, 0, 7) m,
(
SELECT SUM(i.s_b_a) sarpu
FROM TBL_C_C_S i
WHERE substr(i.datetime, 0, 7) = substr(t.datetime, 0, 7)
AND i.datetime <= t.datetime
AND i.fk_c_c_id = t.fk_c_c_id
GROUP BY SUBSTR(i.datetime, 0, 7)
) s
FROM TBL_C_C_S t
Run Code Online (Sandbox Code Playgroud)
我怎么能将这个SQL查询转换为elasticsearch
这是我在弹性搜索中的方式
POST /c_c_s_index_test/_search
{ "size":0,
"aggs": {
"customer": {
"terms": {
"field": "fk_c_c_id",
"size": 5
},
"aggs": {
"sumscore": {
"sum": {
"field": "s_b_a"
}
},
"month": {
"date_histogram": {
"field": "datetime",
"interval": "1M",
"min_doc_count": 1
},
"aggs": {
"customer": {
"sum": {
"field": "s_b_a"
}
}
}
}
}
} ,
"stats_monthly_sales": {
"extended_stats_bucket": {
"buckets_path": "customer>sumscore"
}
}
}
Run Code Online (Sandbox Code Playgroud)
但这只是返回月份的总和,而i.datetime <= t.datetime在此不存在
您可能需要的是累积总和聚合:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-cumulative-sum-aggregation.html
所以你的查询应该如下所示:
{
"size": 0,
"aggs": {
"customer": {
"terms": {
"field": "fk_c_c_id",
"size": 5
},
"aggs": {
"sales_per_month": {
"date_histogram": {
"field": "datetime",
"interval": "month"
},
"aggs": {
"sales": {
"sum": {
"field": "s_b_a"
}
},
"cumulative_sales": {
"cumulative_sum": {
"buckets_path": "sales"
}
}
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
594 次 |
| 最近记录: |