0

我有一张有四列的桌子;Plant_Id、年、月和 MR。我希望能够计算 MR 的范围(2 个月的差异)。表示例(MaintenanceRatebepaen 是表名)如下所示:

Plant_ID  Year  Month  MR  
CCAR      2009    1    0,706452  
CCAR      2009    2    0,625899  
CCAR      2009    3    0,636678  
CCAR      2009    4    0,736544  
CCAR      2009    5    0,552023
CCAR      2009    6    0,418338  
CCAR      2009    7    0,502732  
CCAR      2009    8    0,64526  
CCAR      2009    9    0,743333  
CCAR      2009    10   0,555556  
CCAR      2009    11   0,297561  
CCAR      2009    12   0,338608  
CCAR      2010    1    0,380783  
Etc.

我不确定这是否有趣,但 Plant_id 可以有 25 个不同的值,从 2008 年到 2012 年,1-12 个月,MR 是一个计算值。

查询如下所示:

SELECT  Plant_Id, Jaar, Maand
    ,   (SUM(Compl) + 0.000) / SUM(Total) AS MR  
FROM (
        SELECT  Plant_Id, Jaar, Late, EarlyJobs, OnTimeJobs, Maand
            ,   SUM(EarlyJobs + OnTimeJobs) AS Compl
            ,   SUM(EarlyJobs) + SUM(Late) + SUM(OnTimeJobs) AS Total 
        FROM    MaintenanceRatebepaen AS MaintenanceRatebepaen_1  
        GROUP BY Plant_Id, Jaar, Maand, Late, OnTimeJobs, EarlyJobs
    ) AS MaintenanceRatebepaen  
WHERE   (Jaar >= 2009) AND (Jaar <= 2011) AND (Plant_Id = 'CCAR')   
GROUP BY Jaar, Plant_Id, Maand  
ORDER BY Plant_Id, Jaar, Maand

我是 SQL 新手,我设法从书籍和谷歌搜索中获得了上述内容。但我无法计算范围,非常感谢任何帮助!

4

1 回答 1

1

你只是想要逐月的差异吗?

数据

declare @data table (PlantId nvarchar(5), [Year] nvarchar(4), [Month] int, MR decimal(10,10))

INSERT @data VALUES 
('CCAR','2009','1','0.706452'),('CCAR','2009','2','0.625899'),('CCAR','2009','3','0.636678'),('CCAR','2009','4','0.736544'),('CCAR','2009','5','0.552023'),('CCAR','2009','6','0.418338'),('CCAR','2009','7','0.502732'),('CCAR','2009','8','0.64526'),('CCAR','2009','9','0.743333'),('CCAR','2009','10','0.555556'),('CCAR','2009','11','0.297561'),('CCAR','2009','12','0.338608'),('CCAR','2010','1','0.380783')

询问

;with cte as (
    SELECT *, ROW_NUMBER() OVER (ORDER BY Year DESC, Month DESC) AS RowNumber  FROM @data
)
select  d1.PlantId
    ,   d1.Year
    ,   d1.Month
    ,   d1.MR
    ,   d1.MR-d2.MR AS [ChangeMRFromPreviousMonth]
from    cte d1
LEFT OUTER JOIN cte d2
    on d2.RowNumber = (d1.RowNumber+1)
order by d1.RowNumber DESC

结果:

PlantId Year Month       MR                                      ChangeMRFromPreviousMonth
------- ---- ----------- --------------------------------------- ---------------------------------------
CCAR    2009 1           0.7064520000                            NULL
CCAR    2009 2           0.6258990000                            -0.0805530000
CCAR    2009 3           0.6366780000                            0.0107790000
CCAR    2009 4           0.7365440000                            0.0998660000
CCAR    2009 5           0.5520230000                            -0.1845210000
CCAR    2009 6           0.4183380000                            -0.1336850000
CCAR    2009 7           0.5027320000                            0.0843940000
CCAR    2009 8           0.6452600000                            0.1425280000
CCAR    2009 9           0.7433330000                            0.0980730000
CCAR    2009 10          0.5555560000                            -0.1877770000
CCAR    2009 11          0.2975610000                            -0.2579950000
CCAR    2009 12          0.3386080000                            0.0410470000
CCAR    2010 1           0.3807830000                            0.0421750000

那是你需要的吗?您的查询包含很多额外的列,所以我不知道您是否想要包括在内?

* 编辑 * 作为对您的评论的回应,您需要将您的查询放在 CTE 内,并带有额外的 RowNumber 列。d1 和 d2 只是该 CTE 的别名。我认为应该这样做:

;with cte as (
    SELECT  Plant_Id, Jaar, Maand
        ,   (SUM(Compl) + 0.000) / SUM(Total) AS MR  
        ,   ROW_NUMBER() OVER (ORDER BY Jaar DESC, Maand DESC) AS RowNumber
    FROM (
        SELECT  Plant_Id, Jaar, Late, EarlyJobs, OnTimeJobs, Maand
            ,   SUM(EarlyJobs + OnTimeJobs) AS Compl
            ,   SUM(EarlyJobs) + SUM(Late) + SUM(OnTimeJobs) AS Total 
        FROM    MaintenanceRatebepaen AS MaintenanceRatebepaen_1  
        GROUP BY Plant_Id, Jaar, Maand, Late, OnTimeJobs, EarlyJobs
    ) AS MaintenanceRatebepaen  
    WHERE   (Jaar >= 2009) AND (Jaar <= 2011) AND (Plant_Id = 'CCAR')   
    GROUP BY Jaar, Plant_Id, Maand  
    ORDER BY Plant_Id, Jaar, Maand
)
select  d1.PlantId
    ,   d1.Jaar
    ,   d1.Maand
    ,   d1.MR
    ,   d1.MR-d2.MR AS [ChangeMRFromPreviousMonth]
from    cte d1
LEFT OUTER JOIN cte d2
    on d2.RowNumber = (d1.RowNumber+1)
order by d1.RowNumber DESC
于 2012-04-20T12:15:15.990 回答