sql查询以查找上个月的值差异

时间:2013-12-12 00:21:44

标签: sql-server sql-server-2008 tsql

我有我的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 |       |            |     |                 |     |              |    |
+------+-------+------------+-----|------------+--- |-----|----+---------| ----

任何人都可以告诉我如何修改这个以实现我的目标。

2 个答案:

答案 0 :(得分: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

上述查询仅用于说明目的。提供的查询可以正确处理样本数据,因为它只包含一年的数据。您应该在on的{​​{1}}子句中应用适当的逻辑来检索输入数据超过一年的数据。

答案 1 :(得分:0)

试试这个:

我用你的数据创建了一个临时表#XYZ,用它来减去前几个月的数据。我必须创建另外两个变量来处理跨越年份。

如果您有任何疑问,请与我们联系

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/'+@Year) ))
DECLARE @DiffMonth  varchar(2)  
set @DiffMonth= DATEPART(mm, DATEADD(m,-1,(@Month+'/1/'+@Year) ))



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;