2013-12-12 50 views
0

我有我的SQL表和查詢中值的差異,如下圖所示:SQL查詢來發現從上月

CREATE TABLE #ABC([Year] INT, [Month] INT, Stores INT); 
CREATE TABLE #DEF([Year] INT, [Month] INT, SalesStores INT); 
CREATE TABLE #GHI([Year] INT, [Month] INT, Products INT); 

INSERT #ABC VALUES (2013,1,1); 
INSERT #ABC VALUES (2013,1,2); 
INSERT #ABC VALUES (2013,2,3); 

INSERT #DEF VALUES (2013,1,4); 
INSERT #DEF VALUES (2013,1,5); 
INSERT #DEF VALUES (2013,2,6); 

INSERT #GHI VALUES (2013,1,7); 
INSERT #GHI VALUES (2013,1,8); 
INSERT #GHI VALUES (2013,2,9); 
INSERT #GHI VALUES (2013,3,10); 

我當前的查詢是

I have @Year and @Month as parameters , both integers , example @Year = '2013' , @Month = '11' 

SELECT T.[Year], 
     T.[Month] 
     -- select the sum for each year/month combination using a correlated subquery (each result from the main query causes another data retrieval operation to be run) 
     , 
     (SELECT SUM(Stores) 
     FROM #ABC 
     WHERE [Year] = T.[Year] 
       AND [Month] = T.[Month]) AS [Sum_Stores], 
     (SELECT SUM(SalesStores) 
     FROM #DEF 
     WHERE [Year] = T.[Year] 
       AND [Month] = T.[Month]) AS [Sum_SalesStores], 
     (SELECT SUM(Products) 
     FROM #GHI 
     WHERE [Year] = T.[Year] 
       AND [Month] = T.[Month]) AS [Sum_Products] 
FROM (
     -- this selects a list of all possible dates. 
     SELECT [Year], 
       [Month] 
     FROM #ABC where [Year] = @Year and [Month] = @Month 
     UNION 
     SELECT [Year], 
       [Month] 
     FROM #DEF where [Year] = @Year and [Month] = @Month 
     UNION 
     SELECT [Year], 
       [Month] 
     FROM #GHI where [Year] = @Year and [Month] = @Month) AS T; 

它返回

+------+-------+------------+-----------------+--------------+ 
| Year | Month | Sum_Stores | Sum_SalesStores | Sum_Products | 
+------+-------+------------+-----------------+--------------+ 
| 2013 |  |   |     |    | 
| 2013 |  |   |     |    | 
| 2013 |  |   |     |    | 
+------+-------+------------+-----------------+--------------+ 

我想要做的是在查詢中添加更多列,以顯示與上個月的差異。如下所示。 示例:Sum_Stores旁邊的Diff顯示Sum_Stores從上個月到本月的差異。

事情是這樣的:

+------+-------+------------+-----------------+-----|-----|---+----------------- 
| Year | Month | Sum_Stores |Diff | Sum_SalesStores |Diff | Sum_Products |Diff| 
+------+-------+------------+-----|------------+----|---- |----+--------------| 
| 2013 |  |   |  |     |  |    | | 
| 2013 |  |   |  |     |  |    | | 
| 2013 |  |   |  |     |  |    | | 
+------+-------+------------+-----|------------+--- |-----|----+---------| ---- 

誰能告訴我怎麼可以修改這個才達到我的目標。

+0

你可以使用答案略加修改版本到你類似的問題在這裏:http://stackoverflow.com/questions/20527213/sql-query -to-創建柱,用算術表達式/ 20528571#20528571 – Mike

回答

0

您可以使用CTE,然後自我加入以獲得所需的結果。

Fiddle Here

;WITH DATA AS (
SELECT T.[Year], 
     T.[Month] 
     -- select the sum for each year/month combination using a correlated subquery (each result from the main query causes another data retrieval operation to be run) 
     , 
     (SELECT SUM(Stores) 
     FROM #ABC 
     WHERE [Year] = T.[Year] 
       AND [Month] = T.[Month]) AS [Sum_Stores], 
     (SELECT SUM(SalesStores) 
     FROM #DEF 
     WHERE [Year] = T.[Year] 
       AND [Month] = T.[Month]) AS [Sum_SalesStores], 
     (SELECT SUM(Products) 
     FROM #GHI 
     WHERE [Year] = T.[Year] 
       AND [Month] = T.[Month]) AS [Sum_Products] 
FROM (
     -- this selects a list of all possible dates. 
     SELECT [Year], 
       [Month] 
     FROM #ABC where [Year] = @Year and [Month] = @Month 
     UNION 
     SELECT [Year], 
       [Month] 
     FROM #DEF where [Year] = @Year and [Month] = @Month 
     UNION 
     SELECT [Year], 
       [Month] 
     FROM #GHI where [Year] = @Year and [Month] = @Month 
) AS T) 

SELECT d1.year,d1.month , 
    d1.Sum_Stores , (isnull(d2.Sum_Stores,0) -d1.Sum_Stores) AS storeDiff , 
    d1.Sum_SalesStores ,(isnull(d2.Sum_SalesStores,0) -d1.Sum_SalesStores) AS salesStoresDiff, 
    d1.Sum_Products , (isnull(d2.Sum_Products,0) -d1.Sum_Products) AS prodDiff 

    -- self joining on month -1 to get previous month data 
FROM DATA AS d1 LEFT OUTER JOIN DATA AS d2 ON d2.month = d1.month -1 

上面的查詢是用於說明目的only.The查詢,因​​爲它包含了只有一年的數據正確的樣本數據提供的作品。您應該在left outer joinon條款中應用恰當的邏輯來檢索輸入數據超過一年的數據。

0

試試這個:

我創建了你的數據一個臨時表#XYZ和使用的減去前幾個月的數據。我必須創建2個更多的變量來照顧跨度年。

如果您有任何疑問,請讓我知道

DECLARE @Year varchar(4) 
SET @Year = '2013' 
DECLARE @Month varchar(2) set @Month = '1' 

DECLARE @DiffYear varchar(4) 
Set @DiffYear = DATEPART(yyyy, DATEADD(m,-1,(@Month+'/1/'[email protected]))) 
DECLARE @DiffMonth varchar(2) 
set @DiffMonth= DATEPART(mm, DATEADD(m,-1,(@Month+'/1/'[email protected]))) 



SELECT T.[Year], 
     T.[Month] 
     -- select the sum for each year/month combination using a correlated subquery (each result from the main query causes another data retrieval operation to be run) 
     , 
     (SELECT SUM(Stores) 
     FROM #ABC 
     WHERE [Year] = T.[Year] 
       AND [Month] = T.[Month]) AS [Sum_Stores], 
     (SELECT SUM(SalesStores) 
     FROM #DEF 
     WHERE [Year] = T.[Year] 
       AND [Month] = T.[Month]) AS [Sum_SalesStores], 
     (SELECT SUM(Products) 
     FROM #GHI 
     WHERE [Year] = T.[Year] 
       AND [Month] = T.[Month]) AS [Sum_Products] 
INTO #XYZ 

FROM (
     -- this selects a list of all possible dates. 
     SELECT [Year], 
       [Month] 
     FROM #ABC --where [Year] = @Year and [Month] = @Month 
     UNION 
     SELECT [Year], 
       [Month] 
     FROM #DEF --where [Year] = @Year and [Month] = @Month 
     UNION 
     SELECT [Year], 
       [Month] 
     FROM #GHI --where [Year] = @Year and [Month] = @Month 
     ) 
     AS T; 







SELECT T.[Year], 
     T.[Month] 
     -- select the sum for each year/month combination using a correlated subquery (each result from the main query causes another data retrieval operation to be run) 
     , 
     (SELECT SUM(Stores) 
     FROM #ABC 
     WHERE [Year] = T.[Year] 
       AND [Month] = T.[Month]) AS [Sum_Stores], 



     ISNULL((SELECT SUM(Stores) 
     FROM #ABC 
     WHERE [Year] = T.[Year] 
       AND [Month] = T.[Month]),0) - ISNULL((SELECT SUM([Sum_Stores]) 
     FROM #XYZ 
     WHERE [Year] = @DiffYear 
       AND [Month] = @DiffMonth),(SELECT SUM(Stores) 
     FROM #ABC 
     WHERE [Year] = T.[Year] 
       AND [Month] = T.[Month])) AS [DIFF_Sum_Stores],  

     (SELECT SUM(SalesStores) 
     FROM #DEF 
     WHERE [Year] = T.[Year] 
       AND [Month] = T.[Month]) AS [Sum_SalesStores], 
     ISNULL((SELECT SUM(SalesStores) 
     FROM #DEF 
     WHERE [Year] = T.[Year] 
       AND [Month] = T.[Month]),0) - 

     ISNULL((SELECT SUM([Sum_SalesStores]) 
     FROM #XYZ 
     WHERE [Year] = @DiffYear 
       AND [Month] = @DiffMonth),(SELECT SUM(SalesStores) 
     FROM #DEF 
     WHERE [Year] = T.[Year] 
       AND [Month] = T.[Month])) AS [DIFF_Sum_SalesStores], 




     (SELECT SUM(Products) 
     FROM #GHI 
     WHERE [Year] = T.[Year] 
       AND [Month] = T.[Month]) AS [Sum_Products], 

     ISNULL((SELECT SUM(Products) 
     FROM #GHI 
     WHERE [Year] = T.[Year] 
       AND [Month] = T.[Month]),0) - 
     ISNULL((SELECT SUM([Sum_Products]) 
     FROM #XYZ 
     WHERE [Year] = @DiffYear 
       AND [Month] = @DiffMonth),(SELECT SUM(Products) 
     FROM #GHI 
     WHERE [Year] = T.[Year] 
       AND [Month] = T.[Month])) AS [Diff_Sum_Products] 


FROM (
     -- this selects a list of all possible dates. 
     SELECT [Year], 
       [Month] 
     FROM #ABC where [Year] = @Year and [Month] = @Month 
     UNION 
     SELECT [Year], 
       [Month] 
     FROM #DEF where [Year] = @Year and [Month] = @Month 
     UNION 
     SELECT [Year], 
       [Month] 
     FROM #GHI where [Year] = @Year and [Month] = @Month 
     ) 
     AS T;