如何使用SQL计算时间序列数据的ADI和COV?

How do I calculate the ADI and COV of time series data using SQL?

我正在尝试使用 SQL 对时间序列数据进行分类。我有超过 3 年的参考数据点数据。所以引用出现了 36 次,每个月一次。有时数量为 0,有时每行可能为 25 甚至更高。我想知道的是如何使用 SQL(尤其是 MSSQL)计算这些方程。

然后,同样的,我想将数据分类为ErraticSmoothLumpy,and/or Intermittent as seen here

Smooth demand (ADI < 1.32 and CV² < 0.49). The demand is very regular in time and in quantity. It is therefore easy to forecast and you won’t have trouble reaching a low forecasting error level.

Intermittent demand (ADI >= 1.32 and CV² < 0.49). The demand history shows very little variation in demand quantity but a high variation in the interval between two demands. Though specific forecasting methods tackle intermittent demands, the forecast error margin is considerably higher.

Erratic demand (ADI < 1.32 and CV² >= 0.49). The demand has regular occurrences in time with high quantity variations. Your forecast accuracy remains shaky.

Lumpy demand (ADI >= 1.32 and CV² >= 0.49). The demand is characterized by a large variation in quantity and in time. It is actually impossible to produce a reliable forecast, no matter which forecasting tools you use. This particular type of demand pattern is unforecastable.

这是生成我正在使用的 table 的查询。

SELECT
distinct
CHAN_ID
,PROD_CD
,CP_REF
,PARENT
,ORDERED_DATE
,QUANTITY
FROM DF_ALL_DEMAND_BY_ROW_V
where parent is not null

CP_REF是我关注的ID

这是前 12 行的示例。

请询问您是否需要更清楚的信息。我的 SQL 技能几乎没有基础。

with data as (
    select
        CP_REF,
        count(*) * 1.0 /
          nullif(count(case when QUANTITY > 0 then 1 end), 0) as ADI,
          stdevp(QUANTITY) / nullif(avg(QUANTITY), 0) as COV
    from DF_ALL_DEMAND_BY_ROW_V
    where parent is not null
    group by CP_REF
)
select
    CP_REF, ADI, COV,
    case
        when ADI <  1.32 and COV <  0.49 then 'Smooth'
        when ADI >= 1.32 and COV <  0.49 then 'Intermittent'
        when ADI <  1.32 and COV >= 0.49 then 'Erratic'
        when ADI >= 1.32 and COV >= 0.49 then 'Lumpy'
        else 'Smooth'
    end as DEMAND
from data;

仔细检查您要使用 stdevp() 而不是 stdev。我希望我对统计知识更了解。