在 SQL 中计算 4 天滚动平均值时出错

Error while calculating 4 Day Rolling Average in SQL

我正在尝试计算 SQL 中的 4 天滚动平均值。我有以下格式的日期。我的table有很多公司。我必须分别为每家公司计算滚动平均数。但我的以下查询是在不区分公司的情况下执行滚动平均。

请更正我的查询。预期输出如下:

  select 
[date],[PX_VOLUME],[Company],
avg([PX_VOLUME]) OVER (ORDER BY [date],[Company] ROWS BETWEEN 3 PRECEDING AND CURRENT ROW) as monthrolling
into dbo.[Equity_Indicators_window]
from dbo.[Equity_Indicators_V1]
group by [Company],[date],[PX_VOLUME]
order by [date]  

输入数据:

date    PX_Volume   Company
2018-06-22  7455031       ABC HOLDINGS
2018-06-22  801629        LEMAITRE VASLA
2018-06-22  23951629         CLUB CORP
2018-06-23  7455041       ABC HOLDINGS
2018-06-23  801631        LEMAITRE VASLA
2018-06-23  23951643         CLUB CORP
2018-06-24  745506        ABC HOLDINGS
2018-06-24  801666        LEMAITRE VASLA
2018-06-24  23951698         CLUB CORP
2018-06-25  7455031       ABC HOLDINGS
2018-06-25  801629        LEMAITRE VASLA
2018-06-25  23951629         CLUB CORP
2018-06-26  7455031       ABC HOLDINGS
2018-06-26  801629        LEMAITRE VASLA
2018-06-26  23951629         CLUB CORP

预期输出:

date          PX_Volume          Company    monthrolling
2018-06-22  7455031       ABC HOLDINGS    7455031
2018-06-22  801629        LEMAITRE VASLA    801629
2018-06-22  23951629         CLUB CORP      23951629
2018-06-23  7455041       ABC HOLDINGS     7455036
2018-06-23  801631        LEMAITRE VASLA    801630
2018-06-23  23951643         CLUB CORP     23951636
2018-06-24  745506        ABC HOLDINGS      5218526
2018-06-24  801666        LEMAITRE VASLA    801642
2018-06-24  23951698         CLUB CORP     23951656.67
2018-06-25  7455031       ABC HOLDINGS     5777652.25
2018-06-25  801629        LEMAITRE VASLA    801638.75
2018-06-25  23951629         CLUB CORP      23951649.75
2018-06-26  7455031       ABC HOLDINGS     5777652.25
2018-06-26  801629        LEMAITRE VASLA    801638.75
2018-06-26  23951629         CLUB CORP     23951649.75

您的查询难以阅读 在这种情况下,我建议您使用 CTE AND 在您的 over 子句中使用 partition by

with c as(
Select 
[date]
,[Company]
,AVG([PX_VOLUME]) AS PX_VOLUME
from dbo.[Equity_Indicators_V1]
group by [Company],[date]

)
SELECT [date]
      ,[PX_VOLUME]
      ,[Company]
      ,avg([PX_VOLUME]) OVER (Partition by [date],[Company] ORDER BY [date],[Company] ROWS BETWEEN 3 PRECEDING AND CURRENT ROW ) as monthrolling
into dbo.[Equity_Indicators_window]    
FROM C 

尽量按部就班

您的问题是 group by 中的 px_volume。我想你打算:

select [date], [Company], sum(px_volume) as px_volume,
       avg(sum(px_volume)) over (partition by company
                                 order by [date]
                                 rows between 3 preceding and current_row
                                ) as monthrolling
into dbo.[Equity_Indicators_window]
from dbo.[Equity_Indicators_V1]
group by [Company], [date]
order by [date] ;

注意 partition by company

如果每个公司每个日期一行,则不需要聚合。那将是:

select [date], Company, px_volume,
       avg(px_volume) over (partition by company
                            order by [date]
                            rows between 3 preceding and current_row
                           ) as monthrolling
into dbo.[Equity_Indicators_window]
from dbo.[Equity_Indicators_V1]
order by [date]     

试试下面的方法(你需要把partition by clause中的company带进去):

SELECT [date], 
       [PX_VOLUME], 
       [Company], 
       ROUND(AVG(CONVERT(NUMERIC(18,2), [PX_VOLUME])) OVER(PARTITION BY COMPANY ORDER BY [date] ROWS BETWEEN 3 PRECEDING AND CURRENT ROW), 2) AS monthrolling
INTO dbo.[Equity_Indicators_window]
FROM dbo.[Equity_Indicators_V1]
GROUP BY [Company], 
         [date], 
         [PX_VOLUME]
ORDER BY [date];