gpt4 book ai didi

sql - 将 sql 转换为 dsl elasticsearch 查询

转载 作者:行者123 更新时间:2023-11-29 02:48:40 25 4
gpt4 key购买 nike

我想将此 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

我如何将此 sql 查询转换为 elasticsearch

这是我在 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"
}
}
}

但这只是返回月份的总和 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"
}
}
}
}
}
}
}
}

关于sql - 将 sql 转换为 dsl elasticsearch 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50324759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com