• [p]I hope I'm not mentioning anything that you are covering in subsequent articles, but when you need row and column totals, averages, or other aggregations, then suddenly the CUBE and ROLLUP operators start to become very useful. To take your example....[/p]

    [font="Courier New"]

    SELECT

       COALESCE(CONVERT(CHAR(4),YEAR),'Sum'),

       STR(SUM(CASE WHEN Quarter = 1 THEN Amount ELSE 0 END),5,1) AS [1st Qtr],

       STR(SUM(CASE WHEN Quarter = 2 THEN Amount ELSE 0 END),5,1) AS [2nd Qtr],

       STR(SUM(CASE WHEN Quarter = 3 THEN Amount ELSE 0 END),5,1) AS [3rd Qtr],

       STR(SUM(CASE WHEN Quarter = 4 THEN Amount ELSE 0 END),5,1) AS [4th Qtr],

            STR(SUM(Amount),5,1) AS Total

    FROM #SomeTable1

    GROUP BY YEAR WITH ROLLUP ORDER BY GROUPING(YEAR)

    /*

         1st Qtr 2nd Qtr 3rd Qtr 4th Qtr Total

    ---- ------- ------- ------- ------- -----

    2006   1.1     1.2     1.3     1.4     5.0

    2007   2.1     2.2     2.3     2.4     9.0

    2008   1.5     0.0     2.3     1.9     5.7

    Sum    4.7     3.4     5.9     5.7    19.7

    */

    [/font]

    [p] I love crosstabs and pivot-tables. Soothing, they are, like knitting.[/p]

    Best wishes,
    Phil Factor