• Apologies for my previous post. It was getting late and I didn't notice the sort order. Although the following will look identical to my previous post to the casual observer, please note that I swapped the Integer Divides and the Modulo to produce the correct sort order.

    WITH

    cteEnumerate AS

    (

    SELECT rs.CollectionDate

    , RowNum = ROW_NUMBER() OVER (ORDER BY rs.CollectionDate DESC)-1

    FROM ( --=== This represents your result set

    SELECT '2013/04/14' UNION ALL

    SELECT '2012/12/14' UNION ALL

    SELECT '2013/04/02' UNION ALL

    SELECT '2013/03/01' UNION ALL

    SELECT '2012/11/02' UNION ALL

    SELECT '2013/02/10' UNION ALL

    SELECT '2013/01/30' UNION ALL

    SELECT '2013/04/15' UNION ALL

    SELECT '2012/11/01'

    ) rs (CollectionDate)

    ) --=== Pivot the data using Modulo to locate the columns

    -- and Integer Division to locate the rows

    SELECT [Column #1] = MAX(CASE WHEN RowNum/3 = 0 THEN CollectionDate ELSE '' END)

    , [Column #2] = MAX(CASE WHEN RowNum/3 = 1 THEN CollectionDate ELSE '' END)

    , [Column #3] = MAX(CASE WHEN RowNum/3 = 2 THEN CollectionDate ELSE '' END)

    FROM cteEnumerate

    GROUP BY RowNum%3

    ORDER BY RowNum%3

    ;

    Results:

    Column #1Column #2Column #3

    2013/04/152013/03/012012/12/14

    2013/04/142013/02/102012/11/02

    2013/04/022013/01/302012/11/01

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)