日期:2014-05-16 浏览次数:20552 次
--drop table t
create table t(date datetime, online numeric(10,2))
insert into t
select '2014-02-11', 56 union all
select '2014-02-12', 57 union all
select '2014-02-13', 58
select SUM(online) / 2.0 'avg'
from
(
select top 2 online
from t
order by date desc
)t
/*
avg
57.500000
*/
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2014-02-24 15:18:30
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([date] datetime,[online] int)
insert [huang]
select '2014-02-11',56 union all
select '2014-02-12',57 union all
select '2014-02-13',58
--------------开始查询--------------------------
SELECT AVG([online]*1.0) FROM (
select TOP 2 [online]
from [huang]
ORDER BY [online] DESC )a
----------------结果----------------------------
/*
---------------------------------------
57.500000
*/
select SUM(online) / 2.0 as 'avg'
from
(
select top 2 *
from t
order by online desc
)t