• mario17 (8/23/2013)


    Dwain,

    trying to reverse eng your super code, is this a place to use Cross Apply ONLY, or could be done with Join.

    I can't get second Cross Apply

    --CROSS APPLY (SELECT n FROM Tally WHERE n BETWEEN 1 AND period+1) c

    inner join #Tally on N between 1 and Period+1

    CROSS APPLY (SELECT [month]=DATEPART(month, a.fromDD+n-1),DD=CAST(a.fromDD+n-1 AS DATE)) d

    Thank again

    M

    INNER JOIN like that should work assuming your Tally table is in a temporary table. I have a preference for CROSS APPLY.

    Are you saying you don't understand what the second CROSS APPLY is doing?

    Basically it just does some intermediate calculations. [month] is just the month number which is used later to group the results. DD is the start date + the tally value (-1 to adjust the offset so first n=0).


    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