27,582
社区成员




CREATE TABLE #GatherRecord(GatherTime Date not null, value int not null)
go
insert into #GatherRecord values ('2013-01-30', 30)
insert into #GatherRecord values ('2013-02-02', 1)
insert into #GatherRecord values ('2013-02-02', 2)
insert into #GatherRecord values ('2013-02-02', 3)
insert into #GatherRecord values ('2013-02-10', 10)
insert into #GatherRecord values ('2013-02-21', 20)
insert into #GatherRecord values ('2013-02-21', 21)
insert into #GatherRecord values ('2013-02-21', 22)
insert into #GatherRecord values ('2013-02-25', 25)
go
--需要列出指定月中每一天的平均值,如果表中没有这一天的值,则取之前最近一天的数据进行补充,没有则为0
--2月份的结果应该是这样
-- Date value
-----------------------------------
-- 2013-02-01 30 ***
-- 2013-02-02 2
-- 2013-02-03 3 ***
-- 2013-02-04 3 ***
-- 2013-02-05 3 ***
-- ....... ***
-- 2013-02-10 10
-- 2013-02-11 10 ***
-- 2013-02-12 10 ***
-- ....... ***
-- 2013-02-21 21
-- 2013-02-22 22 ***
-- 2013-02-23 22 ***
-- 2013-02-24 22 ***
-- 2013-02-25 25
-- 2013-02-26 25 ***
-- 2013-02-27 25 ***
-- 2013-02-28 25 ***
-- ***是补齐的数据
CREATE TABLE #GatherRecord(id int IDENTITY(1,1),GatherTime Date not null, value int not null)
go
insert into #GatherRecord values ('2013-01-30', 30)
insert into #GatherRecord values ('2013-02-02', 1)
insert into #GatherRecord values ('2013-02-02', 2)
insert into #GatherRecord values ('2013-02-02', 3)
insert into #GatherRecord values ('2013-02-10', 10)
insert into #GatherRecord values ('2013-02-21', 20)
insert into #GatherRecord values ('2013-02-21', 21)
insert into #GatherRecord values ('2013-02-21', 22)
insert into #GatherRecord values ('2013-02-25', 25)
go
;with cte as (
select dateadd(day,-a.number ,'2013-02-28') as GatherTime,
value = ISNULL(t.value ,(select top 1 value from #GatherRecord
where GatherTime < dateadd(day,-a.number ,'2013-02-28')
order by GatherTime desc,id desc
))
from master..spt_values a left join #GatherRecord t
on t.GatherTime = dateadd(day,-a.number ,'2013-02-28')
where a.TYPE ='p'
)
select GatherTime,AVG(value) as value
from cte
where GatherTime between '2013-02-01' and '2013-02-28'
group by GatherTime
order by GatherTime
--结果
GatherTime value
2013-02-01 00:00:00.000 30
2013-02-02 00:00:00.000 2
2013-02-03 00:00:00.000 3
2013-02-04 00:00:00.000 3
2013-02-05 00:00:00.000 3
2013-02-06 00:00:00.000 3
2013-02-07 00:00:00.000 3
2013-02-08 00:00:00.000 3
2013-02-09 00:00:00.000 3
2013-02-10 00:00:00.000 10
2013-02-11 00:00:00.000 10
2013-02-12 00:00:00.000 10
2013-02-13 00:00:00.000 10
2013-02-14 00:00:00.000 10
2013-02-15 00:00:00.000 10
2013-02-16 00:00:00.000 10
2013-02-17 00:00:00.000 10
2013-02-18 00:00:00.000 10
2013-02-19 00:00:00.000 10
2013-02-20 00:00:00.000 10
2013-02-21 00:00:00.000 21
2013-02-22 00:00:00.000 22
2013-02-23 00:00:00.000 22
2013-02-24 00:00:00.000 22
2013-02-25 00:00:00.000 25
2013-02-26 00:00:00.000 25
2013-02-27 00:00:00.000 25
2013-02-28 00:00:00.000 25
CREATE TABLE #GatherRecord(dataid int identity , GatherTime Date not null, value int not null)
go
insert into #GatherRecord values ('2013-01-30 00:00:00', 30)
insert into #GatherRecord values ('2013-02-02 00:00:00', 1)
insert into #GatherRecord values ('2013-02-02 11:00:00', 2)
insert into #GatherRecord values ('2013-02-02 22:00:00', 3)
insert into #GatherRecord values ('2013-02-10 00:00:00', 10)
insert into #GatherRecord values ('2013-02-21 13:00:00', 20)
insert into #GatherRecord values ('2013-02-21 15:00:00', 21)
insert into #GatherRecord values ('2013-02-21 18:00:00', 22)
insert into #GatherRecord values ('2013-02-25 00:00:00', 25)
CREATE TABLE #GatherRecord(GatherTime Date not null, value int not null)
go
insert into #GatherRecord values ('2013-01-30 00:00:00', 30)
insert into #GatherRecord values ('2013-02-02 00:00:00', 1)
insert into #GatherRecord values ('2013-02-02 11:00:00', 2)
insert into #GatherRecord values ('2013-02-02 22:00:00', 3)
insert into #GatherRecord values ('2013-02-10 00:00:00', 10)
insert into #GatherRecord values ('2013-02-21 13:00:00', 20)
insert into #GatherRecord values ('2013-02-21 15:00:00', 21)
insert into #GatherRecord values ('2013-02-21 18:00:00', 22)
insert into #GatherRecord values ('2013-02-25 00:00:00', 25)
现在关键在于 2013-02-03 到2013-02-09 的值应该是 3 ,也就是用2号最后一条记录来补充,而不是2号的平均值。