1

For each vendor for each month values in Jan - Dec: calculate moving averages in columns from 2 - 12 months (i.e., 2 month moving average, 3 month moving average,..., 12 month moving average)

***if current month does not have enough preceding rows to calculate (i) month moving average then record as 0.

Current code works with case statements using row_number() to make sure there are enough preceding rows to calculate average. Looking for an alternative way to shorten code, maybe with For Loop?

Results should look like the following output.

SELECT
    O.*,  
    CASE WHEN ROW_NUM > 1 THEN ROUND(AVG("Adj Fill Rate") OVER (PARTITION BY "Vendor Code" ORDER BY "Vendor Code", "Year Month" ROWS 1 PRECEDING), 6) ELSE 0 END AS "2 Mo OTD Avg",
    CASE WHEN ROW_NUM > 2 THEN ROUND(AVG("Adj Fill Rate") OVER (PARTITION BY "Vendor Code" ORDER BY "Vendor Code", "Year Month" ROWS 2 PRECEDING), 6) ELSE 0 END AS "3 Mo OTD Avg", 
    CASE WHEN ROW_NUM > 3 THEN ROUND(AVG("Adj Fill Rate") OVER (PARTITION BY "Vendor Code" ORDER BY "Vendor Code", "Year Month" ROWS 3 PRECEDING), 6) ELSE 0 END AS "4 Mo OTD Avg", 
    CASE WHEN ROW_NUM > 4 THEN ROUND(AVG("Adj Fill Rate") OVER (PARTITION BY "Vendor Code" ORDER BY "Vendor Code", "Year Month" ROWS 4 PRECEDING), 6) ELSE 0 END AS "5 Mo OTD Avg", 
    CASE WHEN ROW_NUM > 5 THEN ROUND(AVG("Adj Fill Rate") OVER (PARTITION BY "Vendor Code" ORDER BY "Vendor Code", "Year Month" ROWS 5 PRECEDING), 6) ELSE 0 END AS "6 Mo OTD Avg", 
    CASE WHEN ROW_NUM > 6 THEN ROUND(AVG("Adj Fill Rate") OVER (PARTITION BY "Vendor Code" ORDER BY "Vendor Code", "Year Month" ROWS 6 PRECEDING), 6) ELSE 0 END AS "7 Mo OTD Avg", 
    CASE WHEN ROW_NUM > 7 THEN  ROUND(AVG("Adj Fill Rate") OVER (PARTITION BY "Vendor Code" ORDER BY "Vendor Code", "Year Month" ROWS 7 PRECEDING), 6) ELSE 0 END AS "8 Mo OTD Avg", 
    CASE WHEN ROW_NUM > 8 THEN  ROUND(AVG("Adj Fill Rate") OVER (PARTITION BY "Vendor Code" ORDER BY "Vendor Code", "Year Month" ROWS 8 PRECEDING), 6) ELSE 0 END AS "9 Mo OTD Avg", 
    CASE WHEN ROW_NUM > 9 THEN  ROUND(AVG("Adj Fill Rate") OVER (PARTITION BY "Vendor Code" ORDER BY "Vendor Code", "Year Month" ROWS 9 PRECEDING), 6) ELSE 0 END AS "10 Mo OTD Avg", 
    CASE WHEN ROW_NUM > 10 THEN  ROUND(AVG("Adj Fill Rate") OVER (PARTITION BY "Vendor Code" ORDER BY "Vendor Code", "Year Month" ROWS 10 PRECEDING), 6) ELSE 0 END AS "11 Mo OTD Avg", 
    CASE WHEN ROW_NUM > 11 THEN  ROUND(AVG("Adj Fill Rate") OVER (PARTITION BY "Vendor Code" ORDER BY "Vendor Code", "Year Month" ROWS 11 PRECEDING), 6) ELSE 0 END AS "12 Mo OTD Avg"  
FROM (
    SELECT
        F.VNDR_CODE AS "Vendor Code",
        TO_CHAR(F.ASOFDT_END, 'YYYY/MM') AS "Year Month",
        ROUND((SUM(CASE WHEN F.TBK_CNT <> 0 AND R.PW_VNDR_FIX = 'P' THEN 1 ELSE 0 END)+SUM(F.FILL_CNT))/SUM(F.BASE_CNT), 6) AS "Adj Fill Rate", 
        ROW_NUMBER() OVER(PARTITION BY F.VNDR_CODE ORDER BY TO_CHAR(F.ASOFDT_END, 'YYYY/MM')) AS ROW_NUM
    FROM
        METRIC_HIST F
    LEFT JOIN MCOFR_RSNCD R ON
        F.MATL = R.MATL_LTRIM_0
        AND F.ASOFDT_END = R.ASOFDT
        AND F.VNDR_CODE = R.VNDR_CODE10
        AND F.SPL_PLANT = R.SPL_PLANT
    WHERE
        F.METRIC_TYPE = 'VENDORS'
        AND F.METRIC_YN = 'Y'
        AND F.EL_MVMT IN ('101', 'LE')
        AND EXTRACT(YEAR FROM F.ASOFDT_END)>2017
        AND F.VNDR_CODE IN ('0000009292', '0000034483')
    GROUP BY
        F.VNDR_CODE,
        TO_CHAR(F.ASOFDT_END, 'YYYY/MM')) O
ORDER BY
    "Vendor Code", 
    "Year Month";
| Vendor Code | Year Month | Adj Fill Rate | 2 Mo OTD Avg | 3 Mo OTD Avg | 4 Mo OTD Avg | 5 Mo OTD Avg | 6 Mo OTD Avg | 7 Mo OTD Avg | 8 Mo OTD Avg | 9 Mo OTD Avg | 10 Mo OTD Avg | 11 Mo OTD Avg | 12 Mo OTD Avg |
|-------------|------------|---------------|--------------|--------------|--------------|--------------|--------------|--------------|--------------|--------------|---------------|---------------|---------------|
| 0000009292  | 2018/01    | 0.980392      | 0            | 0            | 0            | 0            | 0            | 0            | 0            | 0            | 0             | 0             | 0             |
| 0000009292  | 2018/02    | 0.906977      | 0.943685     | 0            | 0            | 0            | 0            | 0            | 0            | 0            | 0             | 0             | 0             |
| 0000009292  | 2018/03    | 0.948718      | 0.927848     | 0.945362     | 0            | 0            | 0            | 0            | 0            | 0            | 0             | 0             | 0             |
| 0000009292  | 2018/04    | 0.912281      | 0.9305       | 0.922659     | 0.937092     | 0            | 0            | 0            | 0            | 0            | 0             | 0             | 0             |
| 0000009292  | 2018/05    | 0.9375        | 0.924891     | 0.932833     | 0.926369     | 0.937174     | 0            | 0            | 0            | 0            | 0             | 0             | 0             |
| 0000009292  | 2018/06    | 0.796296      | 0.866898     | 0.882026     | 0.898699     | 0.900354     | 0.913694     | 0            | 0            | 0            | 0             | 0             | 0             |
| 0000009292  | 2018/07    | 0.861538      | 0.828917     | 0.865111     | 0.876904     | 0.891267     | 0.893885     | 0.906243     | 0            | 0            | 0             | 0             | 0             |
| 0000009292  | 2018/08    | 0.98          | 0.920769     | 0.879278     | 0.893834     | 0.897523     | 0.906056     | 0.906187     | 0.915463     | 0            | 0             | 0             | 0             |
| 0000009292  | 2018/09    | 0.902778      | 0.941389     | 0.914772     | 0.885153     | 0.895622     | 0.898399     | 0.905587     | 0.905761     | 0.914053     | 0             | 0             | 0             |
| 0000009292  | 2018/10    | 0.928571      | 0.915675     | 0.937116     | 0.918222     | 0.893837     | 0.901114     | 0.902709     | 0.90846      | 0.908295     | 0.915505      | 0             | 0             |
| 0000009292  | 2018/11    | 0.954545      | 0.941558     | 0.928631     | 0.941474     | 0.925486     | 0.903955     | 0.908747     | 0.909189     | 0.913581     | 0.91292       | 0.919054      | 0             |
| 0000009292  | 2018/12    | 0.895833      | 0.925189     | 0.926316     | 0.920432     | 0.932345     | 0.920544     | 0.902794     | 0.907133     | 0.907705     | 0.911806      | 0.911367      | 0.917119      |
| 0000009292  | 2019/01    | 0.904762      | 0.900298     | 0.91838      | 0.920928     | 0.917298     | 0.927748     | 0.91829      | 0.90304      | 0.906869     | 0.90741       | 0.911166      | 0.910817      |
| 0000009292  | 2019/02    | 0.84          | 0.872381     | 0.880198     | 0.898785     | 0.904742     | 0.904415     | 0.915213     | 0.908503     | 0.896036     | 0.900182      | 0.901282      | 0.905235      |
| 0000009292  | 2019/03    | 0.918919      | 0.87946      | 0.887894     | 0.889879     | 0.902812     | 0.907105     | 0.906487     | 0.915676     | 0.909661     | 0.898324      | 0.901886      | 0.902752      |
| 0000009292  | 2019/04    | 0.880597      | 0.899758     | 0.879839     | 0.88607      | 0.888022     | 0.899109     | 0.903318     | 0.903251     | 0.911778     | 0.906754      | 0.896713      | 0.900112      |
| 0000009292  | 2019/05    | 0.939394      | 0.909996     | 0.91297      | 0.894728     | 0.896734     | 0.896584     | 0.904864     | 0.907828     | 0.907267     | 0.91454       | 0.909722      | 0.900269      |
| 0000009292  | 2019/06    | 0.84507       | 0.892232     | 0.888354     | 0.895995     | 0.884796     | 0.888124     | 0.889225     | 0.89739      | 0.900855     | 0.901047      | 0.908224      | 0.904334      |
| 0000009292  | 2019/07    | 0.739726      | 0.792398     | 0.841397     | 0.851197     | 0.864741     | 0.860618     | 0.866924     | 0.870538     | 0.879872     | 0.884742      | 0.886381      | 0.894183      |
| 0000009292  | 2019/08    | 0.541667      | 0.640697     | 0.708821     | 0.766464     | 0.789291     | 0.810896     | 0.815053     | 0.826267     | 0.833996     | 0.846051      | 0.853553      | 0.857655      |
| 0000034483  | 2018/01    | 0.269841      | 0            | 0            | 0            | 0            | 0            | 0            | 0            | 0            | 0             | 0             | 0             |
| 0000034483  | 2018/02    | 0.322314      | 0.296078     | 0            | 0            | 0            | 0            | 0            | 0            | 0            | 0             | 0             | 0             |
| 0000034483  | 2018/03    | 0.29661       | 0.309462     | 0.296255     | 0            | 0            | 0            | 0            | 0            | 0            | 0             | 0             | 0             |
| 0000034483  | 2018/04    | 0.279221      | 0.287916     | 0.299382     | 0.291997     | 0            | 0            | 0            | 0            | 0            | 0             | 0             | 0             |
| 0000034483  | 2018/05    | 0.379032      | 0.329127     | 0.318288     | 0.319294     | 0.309404     | 0            | 0            | 0            | 0            | 0             | 0             | 0             |
| 0000034483  | 2018/06    | 0.308943      | 0.343988     | 0.322399     | 0.315952     | 0.317224     | 0.309327     | 0            | 0            | 0            | 0             | 0             | 0             |
| 0000034483  | 2018/07    | 0.345679      | 0.327311     | 0.344551     | 0.328219     | 0.321897     | 0.321967     | 0.31452      | 0            | 0            | 0             | 0             | 0             |
| 0000034483  | 2018/08    | 0.263566      | 0.304623     | 0.306063     | 0.324305     | 0.315288     | 0.312175     | 0.313624     | 0.308151     | 0            | 0             | 0             | 0             |
| 0000034483  | 2018/09    | 0.258278      | 0.260922     | 0.289174     | 0.294117     | 0.3111       | 0.305787     | 0.304476     | 0.306705     | 0.302609     | 0             | 0             | 0             |
| 0000034483  | 2018/10    | 0.285714      | 0.271996     | 0.269186     | 0.288309     | 0.292436     | 0.306869     | 0.302919     | 0.30213      | 0.304373     | 0.30092       | 0             | 0             |
| 0000034483  | 2018/11    | 0.292683      | 0.289199     | 0.278892     | 0.27506      | 0.289184     | 0.292477     | 0.304842     | 0.30164      | 0.301081     | 0.303204      | 0.300171      | 0             |
| 0000034483  | 2018/12    | 0.453947      | 0.373315     | 0.344115     | 0.322656     | 0.310838     | 0.316645     | 0.315544     | 0.32348      | 0.318563     | 0.316367      | 0.316908      | 0.312986      |
| 0000034483  | 2019/01    | 0.473214      | 0.463581     | 0.406615     | 0.37639      | 0.352767     | 0.3379       | 0.339012     | 0.335253     | 0.340117     | 0.334028      | 0.330626      | 0.329933      |
| 0000034483  | 2019/02    | 0.42          | 0.446607     | 0.449054     | 0.409961     | 0.385112     | 0.363973     | 0.349629     | 0.349135     | 0.344669     | 0.348106      | 0.341843      | 0.338074      |
| 0000034483  | 2019/03    | 0.227586      | 0.323793     | 0.3736       | 0.393687     | 0.373486     | 0.358857     | 0.344489     | 0.334374     | 0.33563      | 0.332961      | 0.337149      | 0.332322      |
| 0000034483  | 2019/04    | 0.258333      | 0.24296      | 0.301973     | 0.344783     | 0.366616     | 0.354294     | 0.344497     | 0.333719     | 0.325925     | 0.3279        | 0.326177      | 0.330581      |
| 0000034483  | 2019/05    | 0.404959      | 0.331646     | 0.296959     | 0.32772      | 0.356818     | 0.373007     | 0.361532     | 0.352055     | 0.341635     | 0.333828      | 0.334905      | 0.332742      |
| 0000034483  | 2019/06    | 0.401869      | 0.403414     | 0.355054     | 0.323187     | 0.342549     | 0.364327     | 0.37713      | 0.366574     | 0.357589     | 0.347658      | 0.340014      | 0.340486      |
| 0000034483  | 2019/07    | 0.317073      | 0.359471     | 0.374634     | 0.345559     | 0.321964     | 0.338303     | 0.357576     | 0.369623     | 0.361074     | 0.353538      | 0.344878      | 0.338102      |
| 0000034483  | 2019/08    | 0.365591      | 0.341332     | 0.361511     | 0.372373     | 0.349565     | 0.329235     | 0.342202     | 0.358578     | 0.369175     | 0.361526      | 0.354634      | 0.346604      |
2
  • 2 to 12 - isn't that eleven columns, not ten? Let's start with that...
    – user5683823
    Aug 25, 2019 at 0:55
  • correct, edit made. use results table as reference for the target output layout. I'm just looking for a way to loop over the case statement instead of writing it out 11 times in Select
    – baodev
    Aug 25, 2019 at 1:33

1 Answer 1

1

The code can be slightly shortened by using a CROSS JOIN and LEVEL to create the 11 extra values, and then using a PIVOT to aggregate them back into a row. This version needs only one analytic function.

select *
from
(
    select
        vendor_code, year_month, fill_rate, n,
        case when row_num > n then round(avg(fill_rate) over (partition by vendor_code, n order by vendor_code, year_month rows n preceding), 6) else 0 end as otd_average
    from
    (
        select vendor_code, year_month, fill_rate,
            row_number() over(partition by vendor_code order by year_month) as row_num
        from vendors
    ) o
    cross join
    (
        select level n
        from dual
        connect by level <= 11
    )
    order by vendor_code, year_month, n
)
pivot(max(otd_average) as rate for (n) in (1,2,3,4,5,6,7,8,9,10,11))
order by vendor_code, year_month;

The above code uses this sample data:

create table vendors as
select 9292  vendor_code, to_date('2018/01', 'YYYY/MM') year_month, 0.980392 fill_rate from dual union all
select 9292  vendor_code, to_date('2018/02', 'YYYY/MM') year_month, 0.906977 fill_rate from dual union all
select 9292  vendor_code, to_date('2018/03', 'YYYY/MM') year_month, 0.948718 fill_rate from dual union all
select 9292  vendor_code, to_date('2018/04', 'YYYY/MM') year_month, 0.912281 fill_rate from dual union all
select 9292  vendor_code, to_date('2018/05', 'YYYY/MM') year_month, 0.9375 fill_rate from dual union all
select 9292  vendor_code, to_date('2018/06', 'YYYY/MM') year_month, 0.796296 fill_rate from dual union all
select 9292  vendor_code, to_date('2018/07', 'YYYY/MM') year_month, 0.861538 fill_rate from dual union all
select 9292  vendor_code, to_date('2018/08', 'YYYY/MM') year_month, 0.98 fill_rate from dual union all
select 9292  vendor_code, to_date('2018/09', 'YYYY/MM') year_month, 0.902778 fill_rate from dual union all
select 9292  vendor_code, to_date('2018/10', 'YYYY/MM') year_month, 0.928571 fill_rate from dual union all
select 9292  vendor_code, to_date('2018/11', 'YYYY/MM') year_month, 0.954545 fill_rate from dual union all
select 9292  vendor_code, to_date('2018/12', 'YYYY/MM') year_month, 0.895833 fill_rate from dual union all
select 9292  vendor_code, to_date('2019/01', 'YYYY/MM') year_month, 0.904762 fill_rate from dual union all
select 9292  vendor_code, to_date('2019/02', 'YYYY/MM') year_month, 0.84 fill_rate from dual union all
select 9292  vendor_code, to_date('2019/03', 'YYYY/MM') year_month, 0.918919 fill_rate from dual union all
select 9292  vendor_code, to_date('2019/04', 'YYYY/MM') year_month, 0.880597 fill_rate from dual union all
select 9292  vendor_code, to_date('2019/05', 'YYYY/MM') year_month, 0.939394 fill_rate from dual union all
select 9292  vendor_code, to_date('2019/06', 'YYYY/MM') year_month, 0.84507 fill_rate from dual union all
select 9292  vendor_code, to_date('2019/07', 'YYYY/MM') year_month, 0.739726 fill_rate from dual union all
select 9292  vendor_code, to_date('2019/08', 'YYYY/MM') year_month, 0.541667 fill_rate from dual union all
select 34483 vendor_code, to_date('2018/01', 'YYYY/MM') year_month, 0.269841 fill_rate from dual union all
select 34483 vendor_code, to_date('2018/02', 'YYYY/MM') year_month, 0.322314 fill_rate from dual union all
select 34483 vendor_code, to_date('2018/03', 'YYYY/MM') year_month, 0.29661 fill_rate from dual union all
select 34483 vendor_code, to_date('2018/04', 'YYYY/MM') year_month, 0.279221 fill_rate from dual union all
select 34483 vendor_code, to_date('2018/05', 'YYYY/MM') year_month, 0.379032 fill_rate from dual union all
select 34483 vendor_code, to_date('2018/06', 'YYYY/MM') year_month, 0.308943 fill_rate from dual union all
select 34483 vendor_code, to_date('2018/07', 'YYYY/MM') year_month, 0.345679 fill_rate from dual union all
select 34483 vendor_code, to_date('2018/08', 'YYYY/MM') year_month, 0.263566 fill_rate from dual union all
select 34483 vendor_code, to_date('2018/09', 'YYYY/MM') year_month, 0.258278 fill_rate from dual union all
select 34483 vendor_code, to_date('2018/10', 'YYYY/MM') year_month, 0.285714 fill_rate from dual union all
select 34483 vendor_code, to_date('2018/11', 'YYYY/MM') year_month, 0.292683 fill_rate from dual union all
select 34483 vendor_code, to_date('2018/12', 'YYYY/MM') year_month, 0.453947 fill_rate from dual union all
select 34483 vendor_code, to_date('2019/01', 'YYYY/MM') year_month, 0.473214 fill_rate from dual union all
select 34483 vendor_code, to_date('2019/02', 'YYYY/MM') year_month, 0.42 fill_rate from dual union all
select 34483 vendor_code, to_date('2019/03', 'YYYY/MM') year_month, 0.227586 fill_rate from dual union all
select 34483 vendor_code, to_date('2019/04', 'YYYY/MM') year_month, 0.258333 fill_rate from dual union all
select 34483 vendor_code, to_date('2019/05', 'YYYY/MM') year_month, 0.404959 fill_rate from dual union all
select 34483 vendor_code, to_date('2019/06', 'YYYY/MM') year_month, 0.401869 fill_rate from dual union all
select 34483 vendor_code, to_date('2019/07', 'YYYY/MM') year_month, 0.317073 fill_rate from dual union all
select 34483 vendor_code, to_date('2019/08', 'YYYY/MM') year_month, 0.365591 fill_rate from dual;

While this literally answers the question I think I prefer your original version. The CROSS JOIN, LEVEL, and PIVOT syntax are all useful, but this feels like a lot of features to throw at an already complicated calculation. Sometimes repetitive dumb code is better than overly-clever code, but that's a subjective decision.

I'm not sure which version will perform better. Both of them only sort the data once, since Oracle is smart enough to know that multiple analytic expressions with the same PARTITION BY clause can be grouped together. (You can verify this in the execution plan, where there are only 2 sort operations.) I'd guess they both perform about the same.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.