SQL中带有汇总子查询的复杂请求
我有一个请求。这是我到目前为止我的代码:
SELECT
client,
indicator,
replace(Month, 'M', '') AS new_Month,---Needed because of the letter M always present with the month column initially
TO_DATE(Year||'-'||LPAD(new_Month,2,'00')||'-01','YYYY-MM-DD')AS Date ,
description,
det_ind.transmitted,
det_ind.export_zip,
amount
FROM indicators as ind
join clients as cl
on ind.code_cl=cl.code_cl
join Detail_indicators as det_ind
on ind.indicator=det_ind.code_ind
代码使我一张表现出的表格:
客户端 | 指示 | 日期 | 说明 | 传输 | export_zip | 金额 |
---|---|---|---|---|---|---|
总计 | 1 2018/11/01 | 布尔 | 一些文本 | 值 | 24544 | ARS1 |
2018/10/10/01 | 此 | 一些 | 文本 | Boolean | Boolean | 14666 |
总计 | ARS1 | 2018/09/01 | 一些文本 | 布尔 | 值布尔值 | 53667 |
ART | ARS1 | 2018/08/01 | 一些文字 | 布尔 | 布尔值布尔值 | 12789 |
式 | ART ARS2 | Boolean | 一些文本布尔值42779总42779 Total ARS2 2018/06/01一些文本 | Boolean | Boolean | 布尔 |
loean | Boolean | 2018/07/01 | Boolean | Boolean | Boolean Boolean Boolean | Boolean |
Boolean | 1122 | 2018 /05/01 | 一些文字 | 布尔布尔 | 值 | 15678 |
总计 | ARS1 | 2018/04/01 | 一些文本 | 文字布尔boolean | boolean | 布尔值21446 |
Total | ARS1 | 2018/03/01 | 一些 | 文本的文本布尔 | boolean | 92556 |
Total | ARS1 | 2018/02/01 | 一些 | boolean Boolean Boolean | Boolean | 13245 |
Apple | 1 | 2018 2018/11 2018/11 /01 | Some text | Boolean | Boolean | 24544 |
APPLE | ARS1 | 2018/10/01 | Some text | Boolean | Boolean | 14666 |
APPLE | ARS1 | 2018/09/01 | Some text | Boolean | Boolean | 53667 |
APPLE | ARS1 | 2018/08/01 | Some text | Boolean | Boolean | 12789 |
APPLE | ARS2 | 2018/07/01 | 一些文本 | 布尔布尔 | 布尔值 | 42779 |
Apple | ARS2 | 2018/06/01 | 一些文本 | 布尔布尔 | 布尔 | 11223 |
Apple | ars1 | 2018/05/01一些文本布尔值15678 Apple Apple ars1 2018/04/01 | 一些文本 | 21446 | 值 | Boolean |
2018/03/01 | Boolean | 文字 | Boolean | Apple | Ars1 | 2018/01 |
一些 | 一些 | Boolean | 文字 | boolean | boolean | 92556 |
然后,我需要计算每个客户端的费率,然后计算出这样的计算:
sum(case when INDICATOR IN ('ARS1') then Amount else 0 end) as Received_money,
sum(case when INDICATOR IN ('ARS2') then Amount else 0 end) as Engaged_money
round(100*sum(Received_money)/sum(Engaged_money),2) as RATE_ENT1
诀窍是,所有具有指示器ENT1的行都需要一个rate_ent1值,该值在与ENT1相关日期之前的4个月中进行了计算,该值将 现在可以实现
add_months(Date,-4)
我的问题是:如何编写此内容以使我的所有ENT1线在一个请求中填充Rate_ent1中的良好结果。
I have a request for you. Here is my code so far:
SELECT
client,
indicator,
replace(Month, 'M', '') AS new_Month,---Needed because of the letter M always present with the month column initially
TO_DATE(Year||'-'||LPAD(new_Month,2,'00')||'-01','YYYY-MM-DD')AS Date ,
description,
det_ind.transmitted,
det_ind.export_zip,
amount
FROM indicators as ind
join clients as cl
on ind.code_cl=cl.code_cl
join Detail_indicators as det_ind
on ind.indicator=det_ind.code_ind
This code yields me a table that is like so:
Client | Indicator | Date | Description | transmitted | export_zip | amount |
---|---|---|---|---|---|---|
TOTAL | ENT1 | 2018/11/01 | Some text | Boolean | Boolean | 24544 |
TOTAL | ARS1 | 2018/10/01 | Some text | Boolean | Boolean | 14666 |
TOTAL | ARS1 | 2018/09/01 | Some text | Boolean | Boolean | 53667 |
TOTAL | ARS1 | 2018/08/01 | Some text | Boolean | Boolean | 12789 |
TOTAL | ARS2 | 2018/07/01 | Some text | Boolean | Boolean | 42779 |
TOTAL | ARS2 | 2018/06/01 | Some text | Boolean | Boolean | 11223 |
TOTAL | ARS1 | 2018/05/01 | Some text | Boolean | Boolean | 15678 |
TOTAL | ARS1 | 2018/04/01 | Some text | Boolean | Boolean | 21446 |
TOTAL | ARS1 | 2018/03/01 | Some text | Boolean | Boolean | 92556 |
TOTAL | ARS1 | 2018/02/01 | Some text | Boolean | Boolean | 13245 |
APPLE | ENT1 | 2018/11/01 | Some text | Boolean | Boolean | 24544 |
APPLE | ARS1 | 2018/10/01 | Some text | Boolean | Boolean | 14666 |
APPLE | ARS1 | 2018/09/01 | Some text | Boolean | Boolean | 53667 |
APPLE | ARS1 | 2018/08/01 | Some text | Boolean | Boolean | 12789 |
APPLE | ARS2 | 2018/07/01 | Some text | Boolean | Boolean | 42779 |
APPLE | ARS2 | 2018/06/01 | Some text | Boolean | Boolean | 11223 |
APPLE | ARS1 | 2018/05/01 | Some text | Boolean | Boolean | 15678 |
APPLE | ARS1 | 2018/04/01 | Some text | Boolean | Boolean | 21446 |
APPLE | ARS1 | 2018/03/01 | Some text | Boolean | Boolean | 92556 |
Then I need to calculate a rate for each Client which is calculated like so:
sum(case when INDICATOR IN ('ARS1') then Amount else 0 end) as Received_money,
sum(case when INDICATOR IN ('ARS2') then Amount else 0 end) as Engaged_money
round(100*sum(Received_money)/sum(Engaged_money),2) as RATE_ENT1
The trick is that all lines with the indicator ENT1 needs a RATE_ENT1 value which is calcultated over the 4 months prior to the date associated with ENT1, which would be implemented with
add_months(Date,-4)
Now my question is: How could I write this to have all my ENT1 lines filled with the good result in RATE_ENT1 in one single request.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论