日期:2014-05-18 浏览次数:20371 次
CREATE TABLE tb ( F_MeterID char(15), F_StartHour datetime, F_EndHour datetime, F_Values numeric(18, 4), F_HourInterval int ) INSERT INTO tb SELECT '350100A10001001', '2011-9-20 11:00', '2011-9-20 15:00', 1239.2, 4 UNION ALL SELECT '350100A10001001', '2011-9-20 20:00', '2011-9-20 23:00', 185.8, 3 UNION ALL SELECT '350100A10001002', '2011-10-13 10:00', '2011-10-13 12:00', 1048, 2 UNION ALL SELECT '350100A10001002', '2011-10-18 08:00', '2011-10-18 11:00', 1521.6, 3 go select F_MeterID , dateadd(hh,n.num,m.F_StartHour) F_StartHour , dateadd(hh,n.num+1,m.F_StartHour) F_StartHour , cast(F_Values / F_HourInterval as decimal(18,2)) F_Values , F_HourInterval = 1 from tb m, (select isnull((select count(1) from sysobjects where id<t.id),0) as num from sysobjects t) n where dateadd(hh,n.num,m.F_StartHour)<=dateadd(hh,-1,m.F_EndHour) order by m.F_MeterID , F_StartHour drop table tb /* F_MeterID F_StartHour F_StartHour F_Values F_HourInterval --------------- ------------------------------------------------------ ------------------------------------------------------ -------------------- -------------- 350100A10001001 2011-09-20 11:00:00.000 2011-09-20 12:00:00.000 309.80 1 350100A10001001 2011-09-20 12:00:00.000 2011-09-20 13:00:00.000 309.80 1 350100A10001001 2011-09-20 13:00:00.000 2011-09-20 14:00:00.000 309.80 1 350100A10001001 2011-09-20 14:00:00.000 2011-09-20 15:00:00.000 309.80 1 350100A10001001 2011-09-20 20:00:00.000 2011-09-20 21:00:00.000 61.93 1 350100A10001001 2011-09-20 21:00:00.000 2011-09-20 22:00:00.000 61.93 1 350100A10001001 2011-09-20 22:00:00.000 2011-09-20 23:00:00.000 61.93 1 350100A10001002 2011-10-13 10:00:00.000 2011-10-13 11:00:00.000 524.00 1 350100A10001002 2011-10-13 11:00:00.000 2011-10-13 12:00:00.000 524.00