最近100个SQL数据库条目的平均值

时间:2013-07-23 18:20:44

标签: sql sql-server tsql stored-procedures

SELECT     DC_GRP.dc_grp, DC_GRPX.reqt_id,
           AVG(RESULTS.[var]) AS Average, STDEV(RESULTS.[var]) AS StdDev
FROM RESULTS INNER JOIN...

目前我正在从结果中提取所有 var 字段,但我只需要获取最新的100.我怎样才能在该AVG()调用中调用最近的100?

1 个答案:

答案 0 :(得分:1)

假设您有一个用于定义最新列的列,您可以使用子查询执行此操作:

SELECT     DC_GRP.dc_grp, DC_GRPX.reqt_id,
           AVG(r.[var]) AS Average, STDEV(r.[var]) AS StdDev
FROM (select top 100 * from RESULTS order by CreatedAt desc) r INNER JOIN...

此示例假定列的名称为CreatedAt