繁体   English   中英

如何在Oracle中通过YTD值计算MTD和QTD

[英]How to calculate MTD and QTD by YTD value in Oracle

我的表t1中有一些数据如下所示:

date    dealer   YTD_Value    
2018-01   A       1100    
2018-02   A       2000
2018-03   A       3000
2018-04   A       4200
2018-05   A       5000
2018-06   A       5500
2017-01   B        100
2017-02   B        200
2017-03   B        500    
...      ...       ...

然后我想编写一个SQL查询此表并获得以下结果:

date    dealer   YTD_Value    MTD_Value       QTD_Value
2018-01   A       1100           1100           1100
2018-02   A       2000            900           2000
2018-03   A       3000           1000           3000
2018-04   A       4200           1200           1200
2018-05   A       5000            800           2000
2018-06   A       5500            500           2500
2017-01   B        100            100            100
2017-02   B        200            100            200
2017-03   B        550            350            550    
...       ...      ...            ...            ...

'YTD'是指年初至今
'MTD'表示迄今为止的月份
'QTD'表示迄今为止的季度

因此,如果我想计算'2018-01'经销商'A' MTDQTD值,则应与YTD相同。

如果我想计算MTD的经销商价值'A''2018-06'MTD值应等于YTD'2018-06'YTD中值'2018-05' 并在QTD值'2018-06'应等于YTD在值'2018-06'减去YTD在值'2018-03'或等于求和MTD中值(2018-04,2018-05,2018-06)

对于其他交易商(例如B)也有相同的规定。

我该如何编写SQL来实现此目的?

QTD的计算很棘手,但是您可以在没有子查询的情况下执行此查询。 基本思想是对月度值进行lag() 然后使用max()分析函数在季度开始时获取YTD值。

当然,一年的第一季度没有这样的值,因此需要coalesce()

尝试这个:

with t(dte, dealer, YTD_Value) as (  
      select '2018-01', 'A', 1100 from dual union all    
      select '2018-02', 'A', 2000 from dual union all
      select '2018-03', 'A', 3000 from dual union all
      select '2018-04', 'A', 4200 from dual union all
      select '2018-05', 'A', 5000 from dual union all
      select '2018-06', 'A', 5500 from dual union all
      select '2017-01', 'B', 100 from dual union all
      select '2017-02', 'B', 200 from dual union all
      select '2017-03', 'B', 550 from dual
   ) 
select t.*,
       (YTD_Value - lag(YTD_Value, 1, 0) over (partition by substr(dte, 1, 4) order by dte)) as MTD_Value,
       (YTD_Value -
        coalesce(max(case when substr(dte, -2) in ('03', '06', '09') then YTD_VALUE end) over
                                   (partition by substr(dte, 1, 4) order by dte rows between unbounded preceding and 1 preceding
                                   ), 0
                )
       ) as QTD_Value
from t
order by 1

是db <>小提琴。

以下查询可以完成这项工作。 它使用CTE将varchar date列转换为日期,然后进行一些联接以恢复要比较的值。

我在此数据库小提琴中对其进行了测试,并且输出与您的预期结果匹配。

WITH cte AS (
    SELECT TO_DATE(my_date, 'YYYY-MM') my_date, dealer, ytd_value FROM my_table
)
SELECT
    TO_CHAR(ytd.my_date, 'YYYY-MM') my_date,
    ytd.ytd_value,
    ytd.dealer,
    ytd.ytd_value - NVL(mtd.ytd_value, 0) mtd_value,
    ytd.ytd_value - NVL(qtd.ytd_value, 0) qtd_value
FROM 
    cte ytd
    LEFT JOIN cte mtd ON mtd.my_date = ADD_MONTHS(ytd.my_date, -1) AND mtd.dealer = ytd.dealer
    LEFT JOIN cte qtd ON qtd.my_date = ADD_MONTHS(TRUNC(ytd.my_date, 'Q'), -1)  AND mtd.dealer = qtd.dealer
ORDER BY dealer, my_date

PS: date是大多数RDBMS(包括Oracle)中的保留字,我在查询中将该列重命名为my_date

您可以sum() over ..聚合函数使用lag() windows analytic和sum() over ..

select "date",dealer,YTD_Value,MTD_Value,
       sum(MTD_Value) over (partition by qt order by "date")
          as QTD_Value 
  from
  (
   with t("date",dealer,YTD_Value) as
   (  
    select '2018-01','A',1100 from dual union all    
    select '2018-02','A',2000 from dual union all
    select '2018-03','A',3000 from dual union all
    select '2018-04','A',4200 from dual union all
    select '2018-05','A',5000 from dual union all
    select '2018-06','A',5500 from dual union all
    select '2017-01','B', 100 from dual union all
    select '2017-02','B', 200 from dual union all
    select '2017-03','B', 550 from dual
   ) 
   select t.*,
          t.YTD_Value - nvl(lag(t.YTD_Value) 
          over (partition by substr("date",1,4) order by substr("date",1,4) desc, "date"),0) 
             as MTD_Value,
          substr("date",1,4)||to_char(to_date("date",'YYYY-MM'),'Q')
             as qt,
          substr("date",1,4) as year
        from t
      order by year desc, "date"
   )     
  order by year desc, "date";

Rextester演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM