我正在尝试利用自我加入来计算 在每个代码清单中每日滚动1年平均值。以下代码似乎提供了所有代码和日期的平均值,而不是提供针对每个代码和日期更改的唯一结果。 (换句话说,输出中的第三列始终相同,但每行实际上应该是唯一的)。
这里有什么建议吗?谢谢
SELECT [Ticker], [Date],
(SELECT AVG(T.[Close])
FROM [intradayOHLC_Selected_Closing_Prices_Staging] AS T
JOIN [intradayOHLC_Selected_Closing_Prices_Staging] AS O2
ON T.[Ticker] = O2.[Ticker]
AND T.[Date] between O2.[Date 250 Days Ago] AND O2.[Date])
FROM [intradayOHLC_Selected_Closing_Prices_Staging] AS O2
答案 0 :(得分:0)
子查询中有一个额外的JOIN
。这不是必需的。你只需要一个相关的子查询。
SELECT [Ticker], [Date],
(SELECT AVG(T.[Close])
FROM [intradayOHLC_Selected_Closing_Prices_Staging] AS T
WHERE T.[Ticker] = O2.[Ticker] AND
T.[Date] between O2.[Date 250 Days Ago] AND O2.[Date]
) as RollingAverage
FROM [intradayOHLC_Selected_Closing_Prices_Staging] AS O2;