CUSUM的TSQL计算

时间:2019-07-02 19:41:04

标签: analysis sql-server-2017

我需要计算值作为像差分析报告的一部分。计算的值基于设定的时间段(基线,例如4周),滞后时间段(先前的时间段,例如2周)。

我已经在excel公式中获得了计算结果,但是我不清楚这些计算如何工作。

我已经计算出滚动平均值和标准偏差-公式...中都使用了这两个值。

我已经在网上搜寻有关帮助以了解用于计算像差/累积值的公式的信息...这是Excel公式:

=MAX(0, IF(K9 > ($I$1*J9), ($I$1*J9/2), K9)+ (C10 - (H10 - 0.5*J10)/J10))

C是观测值,K IS CUSUM,I1是阈值(常数),J是滚动标准偏差,I是观察值-期望值,H是期望值(以滚动平均值计算)。

我知道这很难看,很抱歉...非常感谢您的帮助!

我已经在网络上搜寻帮助,而且相对没有效果...

-这将模拟excel文件并计算滚动平均值

-和基于给定时间单位,滞后和基线的标准偏差

DECLARE     
    @Threshold AS NUMERIC(10,2) = 2.4,
    @MinTimeInterval AS INT = 2, -- TimeUnit
    @Baseline AS INT = 4,
    @Lag AS INT = 1,
    @Reference AS DECIMAL(12,2) = 0.5

DECLARE @Start INT = @Lag + @Baseline -- Start Period
DECLARE @End INT = @Lag + 1 -- End Period

DECLARE  @RawTable TABLE
(
    RowID INT IDENTITY NOT NULL,
    intValue INT

)

DECLARE @ReportTable TABLE
(
    RowID INT,
    Observed INT,
    Expected DECIMAL(10,2),
    StandardDeviation DECIMAL(10,2)
)

-- Populate the raw table with sample data
INSERT INTO @RawTable (intValue)

SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 0 UNION ALL  SELECT 1 UNION ALL SELECT 2
UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 2 UNION ALL SELECT 7 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 1 UNION ALL  SELECT 3 UNION ALL SELECT 1


INSERT INTO @ReportTable 

SELECT 
    x.RowID,
    x.intValue AS Observed,
    (SELECT CAST(AVG(CAST(b.intValue AS DECIMAL(12,2))) AS DECIMAL(12,2)) FROM @RawTable b WHERE b.RowID BETWEEN x.RowID - @Start AND x.RowID - @End) AS Expected,
    (SELECT CAST(STDEV(CAST(s.intValue AS DECIMAL(12,2))) AS DECIMAL(12,2)) FROM @RawTable s WHERE s.RowID BETWEEN x.RowID - @Start AND x.RowID - @End) AS RollingSTDEV

FROM @RawTable x

SELECT * FROM @ReportTable

enter image description here

0 个答案:

没有答案