如何查找下一季度值的总和

时间:2016-03-10 08:46:02

标签: sql sql-server sql-server-2012

请查看下面的表格结构和数据

CREATE TABLE Table1
    ([FORECAST_YEAR] int, [FORECAST_MONTH] int, [QUARTER] int, [ITEM_MASTER_SID] int, [COMPANY_MASTER_SID] int, [UNITS] int)
;

INSERT INTO Table1
    ([FORECAST_YEAR], [FORECAST_MONTH], [QUARTER], [ITEM_MASTER_SID], [COMPANY_MASTER_SID], [UNITS])
VALUES
    (2016, 1, 1, 18, 172496, 10000),
    (2016, 2, 1, 18, 172496, 11000),
    (2016, 3, 1, 18, 172496, 12000),
    (2016, 4, 2, 18, 172496, 13000),
    (2016, 5, 2, 18, 172496, 14000),
    (2016, 6, 2, 18, 172496, 15000),
    (2016, 7, 3, 18, 172496, 16000),
    (2016, 8, 3, 18, 172496, 17000),
    (2016, 9, 3, 18, 172496, 18000),
    (2016, 10, 4, 18, 172496, 19000),
    (2016, 11, 4, 18, 172496, 20000),
    (2016, 12, 4, 18, 172496, 21000)
;

现在,对于每个company_master_sid和item_master_sid,我必须计算当前值 季度单位和下个季度单位。 为此,我使用了以下查询

SELECT a.FORECAST_YEAR,
       a.FORECAST_MONTH,
       a.QUARTER,
       a.COMPANY_MASTER_SID,
       a.ITEM_MASTER_SID,
       A.UNITS,
       a.PRESENT_QUARTER_VALUE,
       A.Next_month_value,
       Sum(Next_month_value)
         OVER(
           partition BY company_master_sid, item_master_sid, quarter,
                                         [FORECAST_YEAR]) AS next_Quarter_value
FROM   (SELECT *,
               Sum(units)
                 OVER(
                   partition BY company_master_sid, item_master_sid,
                                  quarter, [FORECAST_YEAR]) AS PRESENT_QUARTER_VALUE,
               Lead(units)
                 OVER(
                   partition BY company_master_sid, item_master_sid, 
                                        forecast_month%3
                   ORDER BY forecast_month) AS Next_month_value
        FROM   #Table1)a
ORDER  BY FORECAST_MONTH 

但是请在单个选择中建议如何简单地进行查询,因为我有两个以上的变量,比如table1中的单位,我还必须找到这些变量的当前和下一个季度值。

My Required Output如下所示

FORECAST_YEAR   FORECAST_MONTH  QUARTER COMPANY_MASTER_SID  ITEM_MASTER_SID UNITS   PRESENT_QUARTER_VALUE   NEXT_MONTH_VALUE    NEXT_QUARTER_VALUE
        2016        1               1       172496                  18      10000       33000                       13000       42000
        2016        2               1       172496                  18      11000       33000                       14000       42000
        2016        3               1       172496                  18      12000       33000                       15000       42000
        2016        4               2       172496                  18      13000       42000                       16000       51000
        2016        5               2       172496                  18      14000       42000                       17000       51000
        2016        6               2       172496                  18      15000       42000                       18000       51000
        2016        7               3       172496                  18      16000       51000                       19000       60000
        2016        8               3       172496                  18      17000       51000                       20000       60000
        2016        9               3       172496                  18      18000       51000                       21000       60000
        2016        10              4       172496                  18      19000       60000                       NULL        NULL
        2016        11              4       172496                  18      20000       60000                       NULL        NULL
        2016        12              4       172496                  18      21000       60000                       NULL        NULL 

以上选择查询返回与发布相同的结果集,请建议将查询简化为单个选择。

2 个答案:

答案 0 :(得分:1)

这有效:演示here

;with cte
 as
 (
select 
*,
sum(units) over (partition by quarter) as quartersum
from 
#table1
)
select cte.*,b.*
from cte
outer apply
(
select units,quartersum from #table1 t2
where cte.quarter+1=t2.quarter
)b

答案 1 :(得分:0)

试试这个

SELECT T1.[FORECAST_YEAR], T1.[FORECAST_MONTH], T1.[QUARTER], T1.[ITEM_MASTER_SID], T1.[COMPANY_MASTER_SID], T1.[UNITS]
        ,SUM(CASE WHEN T2.[QUARTER] = T1.[QUARTER] THEN T2.[UNITS] ELSE 0 END ) PRESENT_QUARTER_VALUE
        ,SUM(CASE WHEN T2.[FORECAST_MONTH] = T1.[FORECAST_MONTH]+1 THEN T2.[UNITS] ELSE 0 END ) NEXT_MONTH_VALUE
        ,SUM(CASE WHEN T2.[QUARTER] = T1.[QUARTER]+1 THEN T2.[UNITS] ELSE 0 END ) NEXT_QUARTER_VALUE
FROM Table1 T1
INNER JOIN Table1 T2 ON T2.[ITEM_MASTER_SID] = T1.[ITEM_MASTER_SID]  AND T2.[COMPANY_MASTER_SID] = T1.[COMPANY_MASTER_SID] 
GROUP BY T1.[FORECAST_YEAR], T1.[FORECAST_MONTH], T1.[QUARTER], T1.[ITEM_MASTER_SID], T1.[COMPANY_MASTER_SID], T1.[UNITS]

或者你可以试试这个

SELECT [FORECAST_YEAR], [FORECAST_MONTH], [QUARTER], [ITEM_MASTER_SID], [COMPANY_MASTER_SID], [UNITS]
        ,(  SELECT SUM([UNITS]) FROM    Table1  T2 
            WHERE T2.[ITEM_MASTER_SID] = T1.[ITEM_MASTER_SID] 
                AND T2.[COMPANY_MASTER_SID] = T1.[COMPANY_MASTER_SID] 
                AND T2.[QUARTER] = T1.[QUARTER])    PRESENT_QUARTER_VALUE
        ,(  SELECT SUM([UNITS]) FROM    Table1  T2 
            WHERE T2.[ITEM_MASTER_SID] = T1.[ITEM_MASTER_SID] 
                AND T2.[COMPANY_MASTER_SID] = T1.[COMPANY_MASTER_SID] 
                AND T2.[FORECAST_MONTH] = T1.[FORECAST_MONTH]+1)    NEXT_MONTH_VALUE
        ,(  SELECT SUM([UNITS]) FROM    Table1  T2 
            WHERE T2.[ITEM_MASTER_SID] = T1.[ITEM_MASTER_SID] 
                AND T2.[COMPANY_MASTER_SID] = T1.[COMPANY_MASTER_SID] 
                AND T2.[QUARTER] = T1.[QUARTER]+1)  NEXT_QUARTER_VALUE
FROM Table1 T1