Reputation: 1014
I am trying to calculate 4 day rolling average in SQL. I have the date in below format. There are many companies in my table. I have to calculate rolling avg separately for each company. But my below query is performing rolling avg without differentiating companies.
How can I correct my query? Expected output below:
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]
Input Data:
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
Expected Output:
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
Upvotes: 0
Views: 131
Reputation: 3744
try the following (you need to take the company in the partition by clause):
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];
Upvotes: 1
Reputation: 1270401
Your problem is the px_volume
in the group by
. I think you intend:
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] ;
Note the partition by company
.
If you have one row per company per date, the aggregation is not needed. That would be:
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]
Upvotes: 1
Reputation: 805
Your query is hard to read In this case i recommend you to use CTE AND In your over clause use 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
Try to do it by steps
Upvotes: 1