• First of all, you should avoid using integers to store dates, even if they are just partial as month and year. You get in lots of trouble when calculating change of years (in this case).

    Here's a possible solution but something else might be better.

    --Parameters

    DECLARE @Year int = 2013,

    @Month int = 2;

    --Variables for previous dates

    DECLARE@PrevYear int,

    @PrevMonth int;

    SELECT @PrevYear = YEAR( DATEADD( MONTH, @Month - 2, CAST( CAST( @Year AS CHAR(4)) AS datetime))),

    @PrevMonth = MONTH( DATEADD( MONTH, @Month - 2, CAST( CAST( @Year AS CHAR(4)) AS datetime)))

    SELECT @Year AS [Year],

    @Month AS [Month],

    MAX( CASE WHEN [Month] = @Month AND [Description] = 'Stores' THEN Value END) AS [SumStores],

    MAX( CASE WHEN [Month] = @Month AND [Description] = 'Stores' THEN Value END) -

    MAX( CASE WHEN [Month] = @PrevMonth AND [Description] = 'Stores' THEN Value END) AS [Diff],

    MAX( CASE WHEN [Month] = @Month AND [Description] = 'SalesStores' THEN Value END) AS [SumStores],

    MAX( CASE WHEN [Month] = @Month AND [Description] = 'SalesStores' THEN Value END) -

    MAX( CASE WHEN [Month] = @PrevMonth AND [Description] = 'SalesStores' THEN Value END) AS [Diff],

    MAX( CASE WHEN [Month] = @Month AND [Description] = 'Products' THEN Value END) AS [SumStores],

    MAX( CASE WHEN [Month] = @Month AND [Description] = 'Products' THEN Value END) -

    MAX( CASE WHEN [Month] = @PrevMonth AND [Description] = 'Products' THEN Value END) AS [Diff]

    FROM (

    -- this selects a list of all possible dates.

    SELECT [Year],

    [Month],

    SUM(Stores) Value,

    'Stores' Description

    FROM #ABC

    where ([Year] = @Year and [Month] = @Month)

    OR ([Year] = @PrevYear and [Month] = @PrevMonth)

    GROUP BY [Year], [Month]

    UNION

    SELECT [Year],

    [Month],

    SUM(SalesStores),

    'SalesStores' Description

    FROM #DEF

    where ([Year] = @Year and [Month] = @Month)

    OR ([Year] = @PrevYear and [Month] = @PrevMonth)

    GROUP BY [Year], [Month]

    UNION

    SELECT [Year],

    [Month],

    SUM(Products),

    'Products' Description

    FROM #GHI

    where ([Year] = @Year and [Month] = @Month)

    OR ([Year] = @PrevYear and [Month] = @PrevMonth)

    GROUP BY [Year], [Month]) AS T

    ;

    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