• Levelling the playing field by adding OPTION (MAXDOP 1) to the crosstab queries (including an additional one of my own design), I got these results:

    Method AVGDuration

    CrossTab11544

    CrossTab CA11746

    CrossTab CTE11610

    Pivot 11389

    A pretty close heat but Pivot seems to have a slight edge.

    CrossTabCA:

    SELECT @Acct = AccountNum,

    @mth = SUM(CASE WHEN Mnth = '2010-01-01' THEN Ammount ELSE 0 END),

    @mth = SUM(CASE WHEN Mnth = '2010-02-01' THEN Ammount ELSE 0 END),

    @mth = SUM(CASE WHEN Mnth = '2010-03-01' THEN Ammount ELSE 0 END),

    @mth = SUM(CASE WHEN Mnth = '2010-04-01' THEN Ammount ELSE 0 END),

    @mth = SUM(CASE WHEN Mnth = '2010-05-01' THEN Ammount ELSE 0 END),

    @mth = SUM(CASE WHEN Mnth = '2010-06-01' THEN Ammount ELSE 0 END),

    @mth = SUM(CASE WHEN Mnth = '2010-07-01' THEN Ammount ELSE 0 END),

    @mth = SUM(CASE WHEN Mnth = '2010-08-01' THEN Ammount ELSE 0 END),

    @mth = SUM(CASE WHEN Mnth = '2010-09-01' THEN Ammount ELSE 0 END),

    @mth = SUM(CASE WHEN Mnth = '2010-10-01' THEN Ammount ELSE 0 END),

    @mth = SUM(CASE WHEN Mnth = '2010-11-01' THEN Ammount ELSE 0 END),

    @mth = SUM(CASE WHEN Mnth = '2010-12-01' THEN Ammount ELSE 0 END)

    FROM #PivotTest

    CROSS APPLY (

    SELECT DATEADD(MM,DATEDIFF(MM,0,TransDate),0) AS Mnth) a

    GROUP BY AccountNum

    OPTION (MAXDOP 1)


    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