日期:2014-05-17 浏览次数:20522 次
create table #tab(时间 datetime,电表编号 varchar(50),度数 int ,倍率 int)
insert into #tab
select '2013-9-11 04:50',0001,100,10 union all
select '2013-9-11 03:30',0001,80,10 union all
select '2013-9-11 05:30', 0001,105,10 union all
select '2013-9-12 010:50',0001,120,10 union all
select '2013-9-13 09:30',0001,180,10 union all
select '2013-9-14 10:30', 0001, 205,10 union all
select '2013-9-11 04:50',0002,100,10 union all
select '2013-9-11 03:30',0002,80,10 union all
select '2013-9-11 05:30',0003,105,10 union all
select '2013-9-12 010:50',0003,120,10 union all
select '2013-9-13 09:30',0003,180,10 union all
select '2013-9-14 10:30',0003, 205,10 union all
select '2013-9-11 05:30',0005, 105,10 union all
select '2013-9-12 010:50',0005,120,10 union all
select '2013-9-13 09:30',0005,180,10 union all
select '2013-9-14 10:30', 0005, 205,10
select * from #tab
if OBJECT_ID('sp_test','p')is not null
drop proc sp_test
go
create proc sp_test
@starttime datetime,
@endtime datetime
as
begin
select 电表编号,sum(度数)度数 from #tab
where 时间 between @starttime and @endtime
group by 电表编号
end
go
exec sp_test'2013-9-1','2013-10-1'
----------------------------------------
电表编号 度数
-------------------------------------------------- -----------
1