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