2013-12-12 2 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); 

나의 현재 쿼리가

+------+-------+------------+-----------------+--------------+ 
| Year | Month | Sum_Stores | Sum_SalesStores | Sum_Products | 
+------+-------+------------+-----------------+--------------+ 
| 2013 |  |   |     |    | 
| 2013 |  |   |     |    | 
| 2013 |  |   |     |    | 
+------+-------+------------+-----------------+--------------+ 
을 반환

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; 

입니다

내가 원하는 것은 지난 달과의 차이를 보여주는 열을 쿼리에 추가하는 것입니다. 아래 그림과 같이. 예 : Sum_Stores 옆의 차이은 지난 달의 Sum_Stores와 이번 달의 차이를 보여줍니다. 이 같은

뭔가 :

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

이 사람이 내 목표를 실현하려이를 수정하는 방법을 말해 줄 수.

+0

당신은 여기 비슷한 질문에 대한 답변의 약간 수정 된 버전을 사용할 수 알려 : http://stackoverflow.com/questions/20527213/sql-query - 생성 - 열과 산술 표현식/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 쿼리 샘플 데이터를 제대로 작품을 제공하는 설명을위한 것입니다. 입력 데이터가 1 ​​년 이상인 데이터를 검색하려면 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;