锯齿型局部极大值的每日总计

csga3l58  于 2021-07-29  发布在  Java
关注(0)|答案(2)|浏览(359)

我有多个单调计数器,可以重设特设。这些计数器在图形化时表现出锯齿行为(但是它们不是严格地递增)。我要一份月报,显示每个柜台的每日最高金额。
到目前为止,我的策略是在计数器小于计数器上一次采样(也小于或等于下一次采样)的行上加一个“1”。然后计算该列上的运行总数,以标识没有重置的序列。
然后,我在每天的时间间隔内分组,计算一天中每个系列的最大最小值,然后将这些部分相加,得到一天的总计。
我所拥有的有用,但运行起来需要10秒。执行计划显示了两大类:一类在ctedata,另一类在cteseries。我觉得我应该能够消除其中一个,但我不知道怎么做。
这段代码的结果是(我现在可以看到它实际上跳过了一个跨越间隔边界的示例):

interval  tagname total
2020-01-01    alpha   3
2020-01-01    bravo   4
2020-01-02    alpha   3
2020-01-02    bravo   4
IF OBJECT_ID('tempdb..#counter_data') IS NOT NULL
    DROP TABLE #counter_data;

CREATE TABLE #counter_data(
    t_stamp DATETIME NOT NULL
    ,tagname VARCHAR(32) NOT NULL
    ,val REAL NULL
    PRIMARY KEY(t_stamp, tagname)
);

INSERT INTO #counter_data(t_stamp, tagname, val)
VALUES
     ('2020-01-01 04:00', 'alpha', 0)
    ,('2020-01-01 04:00', 'bravo', 0)
    ,('2020-01-01 08:00', 'alpha', 1)
    ,('2020-01-01 08:00', 'bravo', 1)
    ,('2020-01-01 12:00', 'alpha', 2)
    ,('2020-01-01 12:00', 'bravo', 2)
    ,('2020-01-01 16:00', 'alpha', 0)
    ,('2020-01-01 16:00', 'bravo', 3)
    ,('2020-01-01 20:00', 'alpha', 1)
    ,('2020-01-01 20:00', 'bravo', 4)
    ,('2020-01-02 04:00', 'alpha', 2)
    ,('2020-01-02 04:00', 'bravo', 5)
    ,('2020-01-02 08:00', 'alpha', 3)
    ,('2020-01-02 08:00', 'bravo', 6)
    ,('2020-01-02 12:00', 'alpha', 0)
    ,('2020-01-02 12:00', 'bravo', 7)
    ,('2020-01-02 16:00', 'alpha', 1)
    ,('2020-01-02 16:00', 'bravo', 8)
    ,('2020-01-02 20:00', 'alpha', 2)
    ,('2020-01-02 20:00', 'bravo', 9)
;

DECLARE @dateStart AS DATETIME = '2020-01-01';
DECLARE @dateEnd AS DATETIME = DATEADD(month, 2, @dateStart);

WITH cteData AS(
    SELECT
        t_stamp
        ,tagname
        ,val
        ,CASE 
            WHEN val < LAG(val) OVER(PARTITION BY tagname ORDER BY t_stamp)
                AND val <= LEAD(val) OVER(PARTITION BY tagname ORDER BY t_stamp)
                THEN 1 
            ELSE 0
            END AS rn
    FROM #counter_data
    WHERE 
        t_stamp >= @dateStart AND t_stamp < @dateEnd
        AND tagname IN(
            'alpha'
            ,'bravo'
        )
)
,cteSeries AS(
    SELECT
        CAST(t_stamp AS DATE) AS interval
        ,tagname
        ,val
        ,SUM(rn) OVER(PARTITION BY tagname ORDER BY t_stamp) AS series
    FROM cteData
)
,cteSubtotal AS(
    SELECT 
        interval
        ,tagname
        ,MAX(val) - MIN(val) AS subtotal
    FROM cteSeries
    GROUP BY interval, tagname, series
)
,cteGrandTotal AS(
    SELECT
        interval
        ,tagname
        ,SUM(subtotal) AS total
    FROM cteSubtotal
    GROUP BY interval, tagname
)
SELECT *
FROM cteGrandTotal
ORDER BY interval, tagname
7xllpg7q

7xllpg7q1#

我只需将每一行中的计数器的增量与前一行进行比较即可计算:

with cte
as
(
    SELECT *,isnull(lag(val) over (partition by tagname order by t_stamp),0) as previousVal
    FROM counter_data
)
SELECT cast(t_stamp as date),tagname, sum(case when val>previousVal then val-previousval else val end )
FROM cte
GROUP BY cast(t_stamp as date),tagname;
krcsximq

krcsximq2#

这看起来像是一个缺口和孤岛问题。我想你想 lag() 获取“previous”值和计算每日计数的条件和。

select 
    tag_name,
    cast(t_stamp as date) t_date,
    sum(case when val = lag_val + 1 the 1 else 0 end) total
from (
    select 
        c.*,
        lag(val) over(
            partition by tagname, cast(t_stamp as date) 
            order by t_stamp
        ) lag_val
    from #counter_data c
) c
group by tagname, cast(t_stamp as date)
order by t_date, tagname

相关问题