Home Forums SQL Server 2008 T-SQL (SS2K8) Pivot Rows to Columns with UnEven amounts of Rows based on Monthly Dates RE: Pivot Rows to Columns with UnEven amounts of Rows based on Monthly Dates

  • I'm not sure if this will help you but it sounds as if you need a calendar table (I used a CTE to get the needed months). I hope this gets you closer to what you need.

    WITH CTE AS(

    SELECT TOP 12 DATEADD( mm, (ROW_NUMBER() OVER(ORDER BY(SELECT NULL)) -1) * -1, DATEADD( mm, DATEDIFF( mm, 0, GETDATE()), 0)) period

    FROM sys.all_columns

    )

    SELECT *

    FROM CTE

    LEFT

    JOIN GPByMonth Cur ON Cur.M = MONTH( period) AND Cur.Y = YEAR( Period)

    LEFT

    JOIN GPByMonth Prev ON Prev.M = MONTH( period)

    AND Prev.Y = YEAR( Period) - 1

    AND Cur.SPRSNSLN = Prev.SPRSNSLN

    ORDER BY Cur.SPRSNSLN , period DESC

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2