• montecarlo2079 (2/21/2013)


    When the select statement runs, it omits about 39 icns in my list. is there something in the join that could be counting these records out?

    Is it possible that [from Date of service] or [to Date of service] is NULL for the missing icns?

    montecarlo2079 (2/21/2013)


    CREATE TABLE #Calendar2(Dt DATETIME NOT NULL PRIMARY KEY);

    ;WITH cte AS

    (

    SELECT CAST('20120101' AS DATETIME) AS c

    UNION ALL

    SELECT DATEADD(dd,1,c) FROM cte

    WHERE c < '20131231'

    )

    INSERT INTO #Calendar2 SELECT c FROM cte OPTION (MAXRECURSION 0);

    Oh my! A recursive counting CTE to create your calendar table? Consider using a Tally table instead! Much faster.

    ;WITH Tally (n) AS (

    SELECT TOP 731 ROW_NUMBER() OVER (ORDER BY (SELECT NULL))-0

    FROM sys.all_columns a CROSS JOIN sys.all_columns b)

    SELECT DATEADD(d, n, '2012-01-01')

    FROM Tally


    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