2

我想计算一年的平均值,情况如下。

 Plant_ID | Year | Month  |  MR   | Range  
  CCAR    | 2009 | 1      | 0,706 | Null  
  CCAR    | 2009 | 2      | 0,626 | 0,08  
  CCAR    | 2009 | 3      | 0,637 | 0,11  
  CCAR    | 2009 | 4      | 0,737 | 0,1  
  CCAR    | 2009 | 5      | 0,552 | 0,19  
  CCAR    | 2009 | 6      | 0,418 | 0,137  
  CCAR    | 2009 | 7      | 0,503 | 0,085  
  CCAR    | 2009 | 8      | 0,645 | 0,058  
  CCAR    | 2009 | 9      | 0,743 | 0,098  
  CCAR    | 2009 | 10     | 0,556 | 0,187  
  CCAR    | 2009 | 11     | 0,298 | 0,258  
  CCAR    | 2009 | 12     | 0,339 | 0,041  
  CCAR    | 2010 | 1      | 0,381 | 0,042  
  Etc.  

我想添加两列;1(aMR) 计算每年的平均 MR(维护率),因此 2009 年的一个平均值,2010 年的一个平均值,以此类推,第二个计算每年的平均范围。我的问题是我不知道在我的查询中在哪里执行此操作,因为它变得有点复杂,我可以处理......我已经做了一个声明来添加一个列 aMR,这是 MR 的平均值(见查询代码如下)。但我无法获得每年的平均值(现在它返回所有 MR 值的总平均值)有人可以帮我解决这个问题吗?

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) 

SELECT d1.Plant_Id, 
       d1.Jaar, 
       d1.Maand, 
       d1.MR, 
       abs(d1.MR - d2.MR) AS [ChangeMRFromPreviousMonth], 
       aMR =(SELECT avg(cte.MR) FROM cte)  
FROM cte d1 
LEFT OUTER JOIN cte d2 ON d2.RowNumber = (d1.RowNumber + 1)  
ORDER BY d1.Rownumber DESC  

所以我想要添加两列;一个计算每年 MR (aMR) 的平均值,第二个计算每年平均范围的列。谁能帮我解决这个问题?我对 SQL 很陌生,任何帮助将不胜感激!

4

3 回答 3

2

如果你能到达这样的桌子myTable (Plant_ID, Year, Month, MR, Range),你已经完成了大部分工作:

SELECT year, AVG(MR) AS "avg_mr", AVG(range) AS "avr_range"
FROM myTable
GROUP BY year;

如果此数据是来自复杂查询的结果,只需为查询替换这个假设的表名:

SELECT year, AVG(MR) AS "avg_mr", AVG(range) AS "avr_range"
FROM (
    SELECT ... FROM ... -- complex query goes here
)
GROUP BY year;
于 2012-04-24T13:53:12.777 回答
0

尝试替换aMR =(SELECT avg(cte.MR) FROM cte)

aMR = avg (d1.MR) over (partition by d1.Plant_Id, d1.Jaar, d1.Maand)

我不确定 d1.Maand。如果它表示月份,请将其从分区中删除。至于范围(或更改?),您也可以尝试:

aChange = avg (d1.MR - d2.MR) over (partition by d1.Plant_Id, d1.Jaar, d1.Maand)
于 2012-04-24T09:44:59.853 回答
0
SELECT 
    PLant_id
    ,year
    ,avg(mr) as avgMR
    ,avg(range) as avgRange
 FROM 
    table
 GROUP BY
  Plant_id,year

然后你可以加入这个来计算方差或类似的

SELECT 
    t.*
    ,c.*
    ,t.mr - c.avgMR as MRvariance
    ,t.Range - c.avgRange as RangeVariance
 FROM
    table as t
        INNER JOIN (
            SELECT 
                PLant_id
                ,year
                ,avg(mr) as avgMR
                ,avg(range) as avgRange
            FROM 
                table
            GROUP BY
                Plant_id,year
      ) as c
          ON c.plant_id = t.plant_id
          AND c.year = t.year
于 2012-04-24T09:52:47.727 回答