我正在尝试根据之前的分区提供排名

I am trying to provide a ranking based on a previous partition

我正在尝试更改排名列,而不是排序 by RMSE ASC 我想 order by RMSESum ASC (这是之前的 partition by)......当我这样做时我得到一个“无法嵌套有序分析函数”的错误对此有什么想法吗?

Select material, plant, fiscper, dmd_final_Fcst, sum_final_dmd_qty, 
row_number() Over (partition by material, plant order by **RMSE ASC**) as **Ranking**,
Sum(RMSE) over (partition by material, plant) as **RMSESum**,
Power(Fcst_er,2) As Sq_er,
dmd_final_fcst - sum_final_dmd_qty As Fcst_Er,
Sqrt(MSE) As RMSE,
Avg(Sq_er) as MSE 
from PRD_DWH_PURCHASING.HistoricalForecastVal
group by 1,2,3,4,5;

您应该使用如下子查询:

SELECT T.*,
       row_number() Over (partition by material, plant order by RMSESum ASC) as Ranking
FROM
(Select material, plant, fiscper, dmd_final_Fcst, sum_final_dmd_qty,
       Sum(RMSE) over (partition by material, plant) as RMSESum,
       Power(Fcst_er,2) As Sq_er,
       dmd_final_fcst - sum_final_dmd_qty As Fcst_Er,
       Sqrt(MSE) As RMSE,
       Avg(Sq_er) as MSE
  from PRD_DWH_PURCHASING.HistoricalForecastVal
 group by 1,2,3,4,5) t;

这样做:

select * from (Select material, plant, fiscper, dmd_final_Fcst, sum_final_dmd_qty, 
row_number() Over (partition by material, plant order by RMSE ASC) as **Ranking**,
Sum(RMSE) over (partition by material, plant) as RMSESum,
Power(Fcst_er,2) As Sq_er,
dmd_final_fcst - sum_final_dmd_qty As Fcst_Er,
Sqrt(MSE) As RMSE,
Avg(Sq_er) as MSE 
from PRD_DWH_PURCHASING.HistoricalForecastVal)
order by RMSESum
;

您可以在不使用子查询的情况下混合使用 window 函数和聚合:

select material, plant, fiscper, dmd_final_Fcst, sum_final_dmd_qty, 
       row_number() Over (partition by material, plant order by Sqrt(MSE)) as Ranking
       sum(RMSE) over (partition by material, plant) as RMSESum,
       Power(Fcst_er, 2) As Sq_er,
       dmd_final_fcst - sum_final_dmd_qty As Fcst_Er,
       Sqrt(MSE) As RMSE,
       Avg(Sq_er) as MSE 
from PRD_DWH_PURCHASING.HistoricalForecastVal
group by 1,2,3,4,5;

但我仍然认为这行不通。您有一堆未聚合的列。我猜你根本不需要聚合并且真的想要:

select material, plant, fiscper, dmd_final_Fcst, sum_final_dmd_qty, 
       row_number() Over (partition by material, plant order by Sqrt(MSE)) as Ranking
       sum(RMSE) over (partition by material, plant) as RMSESum,
       Power(Fcst_er, 2) As Sq_er,
       dmd_final_fcst - sum_final_dmd_qty As Fcst_Er,
       Sqrt(MSE) As RMSE,
       Avg(Sq_er) over (partition by material, plant, fiscper, dmd_final_Fcst, sum_final_dmd_qty) as MSE 
from PRD_DWH_PURCHASING.HistoricalForecastVal;