• ChrisM@Work (10/8/2012)


    dwain.c (10/8/2012)


    <<snip>>

    Nagaram's is the fastest so far - it's a good 20% faster than mine. I'm having a look to find out why - probably the tally-table generation. Yours does an update (and very quickly too) so it's a bit "oranges and apples".

    After I removed the ORDER BY yours and Nagaram's tied. Interesting that you think his should be faster.

    If I'd have been able to get my Quirky Merge to work, I'd have won. 😀

    It might be this lappy 😉

    Had some fun playing with the virtual tally tables. Nagaram's was definitely faster, but not by much;

    PRINT 'Nagaram (reformatted) =================================================='

    SET STATISTICS IO, TIME ON

    ;WITH DigitsCTE AS (SELECT digit FROM (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) AS D(digit))

    , AllDatesCTE AS (

    SELECT date = DATEADD(DAY, N.number - 1, T.min_date)

    FROM (SELECT min_date = MIN(T.DATECOL), max_date = MAX(T.DATECOL) FROM #SAMPLETABLE AS T) AS T

    CROSS APPLY (

    SELECT TOP(DATEDIFF(DAY, T.min_date, T.max_date) + 1)

    number = ROW_NUMBER() OVER(ORDER BY (SELECT NULL))

    FROM DigitsCTE AS D0, DigitsCTE AS D1, DigitsCTE AS D2,

    DigitsCTE AS D3, DigitsCTE AS D4, DigitsCTE AS D5) AS N

    ) SELECT * FROM AllDatesCTE

    SET STATISTICS IO, TIME OFF

    PRINT ''

    PRINT 'ChrisM original ========================================================'

    SET STATISTICS IO, TIME ON

    ;WITH WholeRange AS (

    SELECT datecol = DATEADD(day,n,d.Startdate)

    FROM (SELECT Startdate = MIN(datecol), daysSpan = 1+DATEDIFF(day,MIN(datecol), MAX(datecol)) FROM #SAMPLETABLE) d

    CROSS APPLY (SELECT TOP(daysSpan) n = ROW_NUMBER() OVER (ORDER BY (SELECT NULL))-1 FROM sys.columns a, sys.columns b) x

    ) SELECT * FROM WholeRange

    SET STATISTICS IO, TIME OFF

    PRINT ''

    PRINT 'ChrisM new ============================================================='

    SET STATISTICS IO, TIME ON

    ;WITH WholeRange AS (

    SELECT x.datecol

    FROM (SELECT Startdate = MIN(datecol), Enddate = MAX(datecol) FROM #SAMPLETABLE) d

    CROSS APPLY (

    SELECT TOP(DATEDIFF(day,Startdate, Enddate) + 1)

    datecol = DATEADD(day,ROW_NUMBER() OVER (ORDER BY (SELECT NULL))-1,d.Startdate)

    FROM

    (VALUES($),($),($),($),($),($),($),($),($),($)) AS e(digit),

    (VALUES($),($),($),($),($),($),($),($),($),($)) AS f(digit),

    (VALUES($),($),($),($),($),($),($),($),($),($)) AS g(digit),

    (VALUES($),($),($),($),($),($),($),($),($),($)) AS h(digit),

    (VALUES($),($),($),($),($),($),($),($),($),($)) AS i(digit)

    ) x

    ) SELECT * FROM WholeRange

    SET STATISTICS IO, TIME OFF

    Edit: fixed quote

    Did you forget something? Like possibly outputting the propagated weights?

    I thought output like this was what we were looking for?

    DECLARE @STDate DATETIME, @EDate DATETIME

    SELECT @STDate=MIN(DATECOL), @EDate=MAX(DATECOL) FROM #SAMPLETABLE

    ;WITH Calendar (n) AS (

    SELECT TOP (1+DATEDIFF(day, @STDate, @EDate))

    DATEADD(day, ROW_NUMBER() OVER (ORDER BY (SELECT NULL))-1, @STDate)

    FROM sys.all_columns a, sys.all_columns b)

    SELECT DATECOL=n

    ,WEIGHTS=CASE WHEN WEIGHTS IS NOT NULL THEN WEIGHTS

    ELSE (

    SELECT TOP 1 WEIGHTS

    FROM #SAMPLETABLE c

    WHERE c.DATECOL < n

    ORDER BY c.DATECOL DESC

    ) END

    FROM Calendar a

    LEFT OUTER JOIN #SAMPLETABLE b ON n = DATECOL

    Not that I think my subquery version will be faster or anything. Just sayin'.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St