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

时间:2019-01-12 09:36:19

标签: sql oracle oracle11g

表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'表示迄今为止的季度

因此,如果我要计算MTD中经销商QTD的{​​{1}}和'A'值,则该值应与'2018-01'相同。

如果我要为YTD中的经销商MTD计算'A'值,则'2018-06'值应等于MTD中的YTD值减去'2018-06'中的YTD值。并且'2018-05'中的QTD值应等于'2018-06'中的YTD值减去'2018-06'中的YTD值或等于( 2018-04,2018-05,2018-06)

对于B等其他经销商,同样的规则。

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

3 个答案:

答案 0 :(得分:2)

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

Here是db <>小提琴。

答案 1 :(得分:1)

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

我在this db fiddle中对其进行了测试,结果与您的预期结果相符。

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

答案 2 :(得分:1)

您可以将lag() Windows分析和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 Demo