SQL中带有汇总子查询的复杂请求

发布于 2025-01-31 11:06:44 字数 3649 浏览 2 评论 0原文

我有一个请求。这是我到目前为止我的代码:

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布尔一些文本24544ARS1
2018/10/10/01一些文本BooleanBoolean14666
总计ARS12018/09/01一些文本布尔值布尔值53667
ARTARS12018/08/01一些文字布尔布尔值布尔值12789
ART ARS2Boolean一些文本布尔值42779总42779 Total ARS2 2018/06/01一些文本BooleanBoolean布尔
loeanBoolean2018/07/01BooleanBooleanBoolean Boolean BooleanBoolean
Boolean11222018 /05/01一些文字布尔布尔15678
总计ARS12018/04/01一些文本文字布尔booleanboolean布尔值21446
TotalARS12018/03/01一些文本的文本布尔boolean92556
TotalARS12018/02/01一些boolean Boolean BooleanBoolean13245
Apple12018 2018/11 2018/11 /01Some textBooleanBoolean24544
APPLEARS12018/10/01Some textBooleanBoolean14666
APPLEARS12018/09/01Some textBooleanBoolean53667
APPLEARS12018/08/01Some textBooleanBoolean12789
APPLEARS22018/07/01一些文本布尔布尔布尔值42779
AppleARS22018/06/01一些文本布尔布尔布尔11223
Applears12018/05/01一些文本布尔值15678 Apple Apple ars1 2018/04/01一些文本21446Boolean
2018/03/01Boolean文字BooleanAppleArs12018/01
一些一些Boolean文字booleanboolean92556

然后,我需要计算每个客户端的费率,然后计算出这样的计算:

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:

ClientIndicatorDateDescriptiontransmittedexport_zipamount
TOTALENT12018/11/01Some textBooleanBoolean24544
TOTALARS12018/10/01Some textBooleanBoolean14666
TOTALARS12018/09/01Some textBooleanBoolean53667
TOTALARS12018/08/01Some textBooleanBoolean12789
TOTALARS22018/07/01Some textBooleanBoolean42779
TOTALARS22018/06/01Some textBooleanBoolean11223
TOTALARS12018/05/01Some textBooleanBoolean15678
TOTALARS12018/04/01Some textBooleanBoolean21446
TOTALARS12018/03/01Some textBooleanBoolean92556
TOTALARS12018/02/01Some textBooleanBoolean13245
APPLEENT12018/11/01Some textBooleanBoolean24544
APPLEARS12018/10/01Some textBooleanBoolean14666
APPLEARS12018/09/01Some textBooleanBoolean53667
APPLEARS12018/08/01Some textBooleanBoolean12789
APPLEARS22018/07/01Some textBooleanBoolean42779
APPLEARS22018/06/01Some textBooleanBoolean11223
APPLEARS12018/05/01Some textBooleanBoolean15678
APPLEARS12018/04/01Some textBooleanBoolean21446
APPLEARS12018/03/01Some textBooleanBoolean92556

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文