Running total for previous 7 rows

  • Hi I need a flag to tell me there is 7 1s in a row per site

    This woud class as a long run

    Anyone get an ideas? i have been trying for some while

    thanks in advance

    SiteMonthYrLongRun
    AJan-161
    AFeb-160
    AMar-160
    AApr-160
    AMay-160
    AJun-160
    AJul-161
    AAug-161
    ASep-161
    BJan-161
    BFeb-160
    BMar-161
    BApr-161
    BMay-161
    BJun-161
    BJul-161
    BAug-161
    BSep-161
  • Are you really using SQL 2008?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • No I'm using 17 but the databases are old and most of the new functionality wont work so I thought id play it safe and put it in 08 forum

    thanks

  • michelle.mabbs - Monday, July 3, 2017 6:32 AM

    Hi I need a flag to tell me there is 7 1s in a row per site

    This woud class as a long run

    Anyone get an ideas? i have been trying for some while

    thanks in advance

    SiteMonthYrLongRun
    AJan-161
    AFeb-160
    AMar-160
    AApr-160
    AMay-160
    AJun-160
    AJul-161
    AAug-161
    ASep-161
    BJan-161
    BFeb-160
    BMar-161
    BApr-161
    BMay-161
    BJun-161
    BJul-161
    BAug-161
    BSep-161


    DECLARE @Streak TABLE
    (
        Site        CHAR(1)
        ,MonthYr    VARCHAR(6)
        ,LongRun    BIT
        
    );

    INSERT INTO @Streak
    VALUES
    ('A','Jan-16',1),
    ('A','Feb-16',0),
    ('A','Mar-16',0),
    ('A','Apr-16',0),
    ('A','May-16',0),
    ('A','Jun-16',0),
    ('A','Jul-16',1),
    ('A','Aug-16',1),
    ('A','Sep-16',1),
    ('A','Oct-16',1),
    ('A','Nov-16',1),
    ('A','Dec-16',1),
    ('A','Jan-17',1),
    ('A','Feb-17',0),
    ('A','Mar-17',0),
    ('A','Apr-17',1),
    ('A','May-17',1),
    ('A','Jun-17',1),
    ('B','Jan-16',1),
    ('B','Feb-16',0),
    ('B','Mar-16',1),
    ('B','Apr-16',1),
    ('B','May-16',1),
    ('B','Jun-16',1),
    ('B','Jul-16',1),
    ('B','Aug-16',1),
    ('B','Sep-16',1),
    ('B','Oct-16',1),
    ('B','Nov-16',1),
    ('B','Dec-16',1),
    ('B','Jan-17',1),
    ('B','Feb-17',1),
    ('B','Mar-17',1),
    ('B','Apr-16',1),
    ('B','May-17',1),
    ('B','Jun-17',1),
    ('B','Jul-17',1)
    ;

    WITH cte_base as
    (
        SELECT
            Site
            ,MonthYr
            ,LongRun
            ,C_Date
            ,CASE
            WHEN LongRun = 1
                THEN
                (
                ROW_NUMBER() OVER(PARTITION BY Site ORDER BY d.C_date ASC)
                -
                ROW_NUMBER() OVER(PARTITION BY Site,LongRun ORDER BY d.C_Date ASC)
                )
            ELSE NULL END AS Difference
        
        FROM
            @Streak
            CROSS APPLY
            (
                SELECT CAST('01 '+REPLACE(MonthYr,'-',' ')AS DATE) AS C_Date
            )d    
    )
    ,cte_run AS
    (

        SELECT
            Site
            ,MonthYr
            ,LongRun
            ,C_Date
            ,CASE WHEN LongRun = 1 THEN ROW_NUMBER() OVER(PARTITION BY site,difference ORDER BY C_Date) ELSE NULL END AS Series
        FROM cte_base
        
    )

    SELECT
        site
        ,MonthYr
        ,LongRun
        ,CASE WHEN Series %7 = 0 THEN 'Longrun' ELSE NULL END AS Flag
    FROM
    cte_run

    ORDER BY
            Site
            ,C_Date;

    This will calculate a 'streak' of results and flag every seven consecutive 1's.  It's based on this[/url] which gives a more full explanation.  It will work on any version higher than 2005.  I've cleaned up the dates in the CROSS APPLY to actually make them dates rather than VARCHAR.


    On two occasions I have been asked, "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" ... I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
    —Charles Babbage, Passages from the Life of a Philosopher

    How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537

  • michelle.mabbs - Monday, July 3, 2017 6:51 AM

    No I'm using 17 but the databases are old and most of the new functionality wont work so I thought id play it safe and put it in 08 forum

    Err.... SQL Server 2017 hasn't been released yet, so if you're using that, I'll be very surprised.

    There're a couple ways to do this, depending on the version of SQL that you have, so what version are you using here?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Neil Burton - Monday, July 3, 2017 7:57 AM

    michelle.mabbs - Monday, July 3, 2017 6:32 AM

    Hi I need a flag to tell me there is 7 1s in a row per site

    This woud class as a long run

    Anyone get an ideas? i have been trying for some while

    thanks in advance

    SiteMonthYrLongRun
    AJan-161
    AFeb-160
    AMar-160
    AApr-160
    AMay-160
    AJun-160
    AJul-161
    AAug-161
    ASep-161
    BJan-161
    BFeb-160
    BMar-161
    BApr-161
    BMay-161
    BJun-161
    BJul-161
    BAug-161
    BSep-161


    DECLARE @Streak TABLE
    (
        Site        CHAR(1)
        ,MonthYr    VARCHAR(6)
        ,LongRun    BIT
        
    );

    INSERT INTO @Streak
    VALUES
    ('A','Jan-16',1),
    ('A','Feb-16',0),
    ('A','Mar-16',0),
    ('A','Apr-16',0),
    ('A','May-16',0),
    ('A','Jun-16',0),
    ('A','Jul-16',1),
    ('A','Aug-16',1),
    ('A','Sep-16',1),
    ('A','Oct-16',1),
    ('A','Nov-16',1),
    ('A','Dec-16',1),
    ('A','Jan-17',1),
    ('A','Feb-17',0),
    ('A','Mar-17',0),
    ('A','Apr-17',1),
    ('A','May-17',1),
    ('A','Jun-17',1),
    ('B','Jan-16',1),
    ('B','Feb-16',0),
    ('B','Mar-16',1),
    ('B','Apr-16',1),
    ('B','May-16',1),
    ('B','Jun-16',1),
    ('B','Jul-16',1),
    ('B','Aug-16',1),
    ('B','Sep-16',1),
    ('B','Oct-16',1),
    ('B','Nov-16',1),
    ('B','Dec-16',1),
    ('B','Jan-17',1),
    ('B','Feb-17',1),
    ('B','Mar-17',1),
    ('B','Apr-16',1),
    ('B','May-17',1),
    ('B','Jun-17',1),
    ('B','Jul-17',1)
    ;

    WITH cte_base as
    (
        SELECT
            Site
            ,MonthYr
            ,LongRun
            ,C_Date
            ,CASE
            WHEN LongRun = 1
                THEN
                (
                ROW_NUMBER() OVER(PARTITION BY Site ORDER BY d.C_date ASC)
                -
                ROW_NUMBER() OVER(PARTITION BY Site,LongRun ORDER BY d.C_Date ASC)
                )
            ELSE NULL END AS Difference
        
        FROM
            @Streak
            CROSS APPLY
            (
                SELECT CAST('01 '+REPLACE(MonthYr,'-',' ')AS DATE) AS C_Date
            )d    
    )
    ,cte_run AS
    (

        SELECT
            Site
            ,MonthYr
            ,LongRun
            ,C_Date
            ,CASE WHEN LongRun = 1 THEN ROW_NUMBER() OVER(PARTITION BY site,difference ORDER BY C_Date) ELSE NULL END AS Series
        FROM cte_base
        
    )

    SELECT
        site
        ,MonthYr
        ,LongRun
        ,CASE WHEN Series %7 = 0 THEN 'Longrun' ELSE NULL END AS Flag
    FROM
    cte_run

    ORDER BY
            Site
            ,C_Date;

    This will calculate a 'streak' of results and flag every seven consecutive 1's.  It's based on this[/url] which gives a more full explanation.  It will work on any version higher than 2005.  I've cleaned up the dates in the CROSS APPLY to actually make them dates rather than VARCHAR.

    Oooh you've done wow thank you ever so much, I'm over the moon

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply