1

我需要帮助编写将平均最近 52 行数据的 select 语句。

如果少于 52 行,它只会计算有多少行的平均值。

我知道 SQL 中的 avg 函数会跳过 null ......但我已经走到了这一步。

SELECT AVG(E.Interest)
from InterestRates E
Where Interest in (select COUNT(Interest) <=52 from InterestRates)

我希望每行数据返回并计算平均 52 行谢谢

4

2 回答 2

7

试试这个:

SELECT AVG(Interest) AS AvgInterest
FROM (
    SELECT TOP 52 E.Interest
    FROM InterestRates E
    ORDER BY DateEntered DESC
) Top52Interests

编辑

根据评论,您可以按身份订购:

SELECT AVG(Interest) AS AvgInterest
FROM (
    SELECT TOP 52 E.Interest
    FROM InterestRates E
    ORDER BY YourIdentityField DESC
) Top52Interests

好消息是这个查询也可以在 SQL 2000 中工作。

于 2010-09-20T14:38:43.627 回答
0

SELECT AVG(E.Interest) FROM
InterestRates E WHERE ROWNUMBER() <= 52 ORDER BY 不管 DESC;

于 2010-09-20T14:40:30.087 回答