在“上一行”和新列中显示列值

时间:2012-04-20 10:09:28

标签: sql

我有一个有四列的桌子; 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的新手,我设法从书籍和谷歌搜索中获得了上述内容。但我无法得到Range计算,非常感谢任何帮助!

1 个答案:

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