我从电表读取数据(电表的当前读数,每小时读一次)存入数据库表 reading 是这样的:
id           流水号(key)
ammeterid    电表字段(int, unique)
readtime     读取时间(datetime)
reading      电表当前读数(int)要能够对电表的用电量进行统计(存入另一个统计表):
1、实时峰谷用电量统计:
如果数据是在晚上11点到早上7点读取的,那么电表的谷电用电量增加(增加量是本次读数和上一次读数的差额),否则电表的峰电用电量增加(增加量是本次读数和上一次读数的差额)。
2、月度统计:
如果时间是一个月的开始,统计上月峰、谷累计用电量及总用电量。问题:
1、可能用触发器能满足我的需要,但我不知道怎样能在每个月开头自动上月的月度统计?
2、实时峰谷用电量统计时电表增加的“增量”怎样方便的得到?
3、怎样设计统计记录表,更加合理?急盼解决方案!谢谢

解决方案 »

  1.   

    有这样的数据(每小时读取一次,电表有若干个。下面数据以一个电表举例。)   id       ammeterid        readtime           reading        increment
    (流水号)   (电表编号)    (读取时间)     (电表当前读数)    (增量)   1           100       2003-11-1 1:00:01       1002             null
       ..          ...       .................       ....             ....
      100          100       2003-11-1 24:00:01      1030             null
       ..          ...       .................       ....             ....
      250          100       2003-11-2 1:00:01       1120             null
       ..          ...       .................       ....             ....我的目的就是先要统计increment(增量),然后根据每条记录的读取时间来计算峰谷电用量,比如在晚上10点到次日7点之间的就是谷电,其他的是峰电。
    一个月的数据这样统计一下。统计该怎样写效率高一点
      

  2.   

    如果是mssql的话,使存储过程是行的吧:)
      

  3.   

    --数据处理:得到增量
    update 表 set increment=reading-isnull((select reading from 表 
    where ammeterid=a.ammeterid and readtime=(
    select max(readtime) from 表 where ammeterid=a.ammeterid and readtime<a.readtime
    )),0)
    from 表 a
    --查询用电高峰期(用电时量最高的2个小时)
    select ammeterid,readtime
    from 表 a 
    where increment in (
    select top 2 increment from 表 where ammeterid=a.ammeterid order by increment
    )
      

  4.   

    --定义测试数据
    declare @tb table(id int identity(1,1),ammeterid varchar(3),readtime datetime,reading int,increment int)
    insert into @tb(ammeterid,readtime,reading)
    select 100,'2003-11-1 1:00:01',1002
    union all select 101,'2003-11-1 2:00:01',1003
    union all select 100,'2003-11-2 3:00:01',1120
    union all select 101,'2003-11-2 4:00:01',1120
    union all select 100,'2003-11-3 2:00:01',1530
    union all select 101,'2003-11-3 1:00:01',1730--数据处理:得到增量
    update @tb set increment=reading-isnull((select reading from @tb 
    where ammeterid=a.ammeterid and readtime=(
    select max(readtime) from @tb where ammeterid=a.ammeterid and readtime<a.readtime
    )),0)
    from @tb a--显示处理结果
    select * from @tb order by ammeterid,readtime--查询用电高峰期(用电时量最高的2个小时)
    select ammeterid,readtime
    from @tb a 
    where increment in (
    select top 2 increment from @tb where ammeterid=a.ammeterid order by increment
    )/*--查询结果
    --增量计算的结果
    id          ammeterid readtime                   reading     increment   
    ----------- --------- -------------------------- ----------- ----------- 
    1           100       2003-11-01 01:00:01.000    1002        1002
    3           100       2003-11-02 03:00:01.000    1120        118
    5           100       2003-11-03 02:00:01.000    1530        410
    2           101       2003-11-01 02:00:01.000    1003        1003
    4           101       2003-11-02 04:00:01.000    1120        117
    6           101       2003-11-03 01:00:01.000    1730        610(所影响的行数为 6 行)----查询用电高峰期的结果
    ammeterid readtime                                               
    --------- ------------------------------------------------------ 
    100       2003-11-02 03:00:01.000
    101       2003-11-02 04:00:01.000
    100       2003-11-03 02:00:01.000
    101       2003-11-03 01:00:01.000(所影响的行数为 4 行)
    --*/
      

  5.   

    select ammeterid,monthstat=convert(varchar(6),readtime,112)
      ,[f-total]=sum(case when convert(varchar,dateadd(hour,3,readtime),108) between '00:00:00' and '10:00:00' then increment else 0 end)
      ,[g-total]=sum(case when convert(varchar,readtime,108) between '07:00:01' and '20:59:59' then increment else 0 end)
    from 表
    group by ammeterid,convert(varchar(6),readtime,112)