2013-12-12 31 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;