简体   繁体   中英

Daily totals for sawtooth pattern local maxima

I have multiple monotonic counters that can be reset ad-hoc. These counters exhibit sawtooth behavior when graphed (however they are not strictly increasing). I want a monthly report showing daily sums of the maxima for each counter.

My strategy so far is to put a '1' on the rows where the counter is less than the previous sampling of the counter (also less than or equal to the next). Then calculate a running total on that column to identify series without resets.

Then I group over the daily intervals to calculate max-min for each series in the day, then sum those portions to get grand totals for the day.

What I have works, but it takes ~10s to run. The execution plan shows two big sorts: one in cteData and I think the other is in cteSeries. I feel like I should be able to eliminate one of them but I'm at a loss how to do it.

The result of this code is (which I can now see is actually skipping a sample across the interval boundary):

 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

I would just calculate the increase of the counter in each row by comparing it to the previous row:

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;

This looks like a gaps-and-islands problem. I think that you want lag() to get the "previous" value and a conditional sum to compute the daily count.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM