Simple Query?

  • I found this in anther forum and thought it would be a simple query, but I'm drawing a complete blank on it (maybe thinking of weekend beers, not sure).  Now it's piqued my curiosity.  Basically trying to have running totals where the dates are within 1 day of each other.  If 2 days, then start over at 0.

    DECLARE @TableDate TABLE (TrxDate date )
    INSERT INTO @TableDate
    (
    TrxDate
    )
    VALUES( '2018-07-13'),( '2018-07-14'),( '2018-07-15'),( '2018-07-17'),( '2018-07-18'),( '2018-07-19'),( '2018-07-20')

    SELECT TrxDate, IIF( ISNULL( DATEDIFF( DAY , LAG( TrxDate ) OVER (ORDER BY TrxDate ),TrxDate ),0) > 1
                        , 0
                    ,ISNULL( DATEDIFF( DAY , LAG( TrxDate ) OVER (ORDER BY TrxDate ),TrxDate ),0)) AS Nw, NULL AS Conut
    FROM @TableDate

    TrxDateNwCount
    2018-07-130NULL
    2018-07-141NULL
    2018-07-151NULL
    2018-07-170NULL
    2018-07-181NULL
    2018-07-191NULL
    2018-07-201NULL

    I need update Count

    TrxDateNwConut
    2018-07-1300
    2018-07-1411
    2018-07-1512
    2018-07-1700
    2018-07-1811
    2018-07-1912
    2018-07-2013

    For better, quicker answers, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Check the following article:
    http://www.sqlservercentral.com/articles/T-SQL/71550/

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Mike01 - Thursday, August 2, 2018 10:34 AM

    I found this in anther forum and thought it would be a simple query, but I'm drawing a complete blank on it (maybe thinking of weekend beers, not sure).  Now it's piqued my curiosity.  Basically trying to have running totals where the dates are within 1 day of each other.  If 2 days, then start over at 0.

    DECLARE @TableDate TABLE (TrxDate date )
    INSERT INTO @TableDate
    (
    TrxDate
    )
    VALUES( '2018-07-13'),( '2018-07-14'),( '2018-07-15'),( '2018-07-17'),( '2018-07-18'),( '2018-07-19'),( '2018-07-20')

    SELECT TrxDate, IIF( ISNULL( DATEDIFF( DAY , LAG( TrxDate ) OVER (ORDER BY TrxDate ),TrxDate ),0) > 1
                        , 0
                    ,ISNULL( DATEDIFF( DAY , LAG( TrxDate ) OVER (ORDER BY TrxDate ),TrxDate ),0)) AS Nw, NULL AS Conut
    FROM @TableDate

    TrxDateNwCount
    2018-07-130NULL
    2018-07-141NULL
    2018-07-151NULL
    2018-07-170NULL
    2018-07-181NULL
    2018-07-191NULL
    2018-07-201NULL

    I need update Count

    TrxDateNwConut
    2018-07-1300
    2018-07-1411
    2018-07-1512
    2018-07-1700
    2018-07-1811
    2018-07-1912
    2018-07-2013

    This is a classic "Islands" problem
    DECLARE @TableDate TABLE (TrxDate date PRIMARY KEY CLUSTERED, Value int NOT NULL);
    INSERT INTO @TableDate ( TrxDate, Value )
    VALUES ('2018-07-13', 50),('2018-07-14', 30),('2018-07-15', 10)
      ,('2018-07-17', 40),('2018-07-18', 60),('2018-07-19', 80),('2018-07-20', 20)
      ,('2018-07-23', 70),('2018-07-24', 90),('2018-07-25', 70);

    WITH cteData AS (
      -- Find the contiguous dates, and Group them together
      SELECT TrxDate, DATEADD(day, -1 * DENSE_RANK() OVER(ORDER BY TrxDate), TrxDate) AS Grouper
      FROM @TableDate
    )
    , cteIslands AS (
      -- Find the Start/End date of each Island
      SELECT Grouper
       , MIN(TrxDate) AS IslandStart
       , MAX(TrxDate) AS IslandEnd
      FROM cteData
      GROUP BY Grouper
    )
    -- Get the running total for each Island
    SELECT data.TrxDate, data.Value
      , RunTot = SUM(data.Value) OVER (PARTITION BY isl.Grouper
                 ORDER BY data.TrxDate
                 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
    FROM @TableDate AS data
    INNER JOIN cteIslands AS isl ON data.TrxDate between isl.IslandStart and isl.IslandEnd
    ORDER BY data.TrxDate;

  • Just for fun, lets add some calculations to the groups
    DECLARE @TableDate TABLE (TrxDate date PRIMARY KEY CLUSTERED, Value int NOT NULL);
    INSERT INTO @TableDate ( TrxDate, Value )
    VALUES ('2018-07-13', 50),('2018-07-14', 30),('2018-07-15', 10)
      ,('2018-07-17', 40),('2018-07-18', 60),('2018-07-19', 80),('2018-07-20', 20)
      ,('2018-07-23', 70),('2018-07-24', 90),('2018-07-25', 70);

    WITH cteData AS (
      -- Find the contiguous dates
      SELECT TrxDate, DATEADD(day, -1 * DENSE_RANK() OVER(ORDER BY TrxDate), TrxDate) AS Grouper
      FROM @TableDate
    )
    , cteIslands AS (
      -- Find the Start/End date of each Island
      SELECT Grouper
       , MIN(TrxDate) AS IslandStart
       , MAX(TrxDate) AS IslandEnd
      FROM cteData
      GROUP BY Grouper
    )
    -- Get the running total for each Island
    SELECT data.TrxDate, data.Value
      , RunTotal = SUM(data.Value) OVER (PARTITION BY isl.Grouper
                  ORDER BY data.TrxDate
                  ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
      , GroupTotal = SUM(data.Value) OVER (PARTITION BY isl.Grouper)
      , PercentOfGroup = 100.0 * data.Value / SUM(data.Value) OVER (PARTITION BY isl.Grouper)
      , RunPercentOfGroup = 100.0 * SUM(data.Value) OVER (PARTITION BY isl.Grouper
                  ORDER BY data.TrxDate
                  ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
             / SUM(data.Value) OVER (PARTITION BY isl.Grouper)
    FROM @TableDate AS data
    INNER JOIN cteIslands AS isl ON data.TrxDate between isl.IslandStart and isl.IslandEnd
    ORDER BY data.TrxDate;

  • Damn you  Dense_Rank. Should've known Jeff wrote something about this.   Thanks for all your help

    For better, quicker answers, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

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

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