0

我需要一个用于仪表系统的光标示例,系统每个月都会读取仪表。

光标需要检查,每个仪表在当年都有一个读数。对于读数缺失的电表,增加一个估计值,使得每日消耗量为上一期的每日消耗量加 15%。在没有前一期间,使用上述 Kwh 值。

4

2 回答 2

1

这样的事情怎么样。(MonthSeed 表可以成为数据库中的真实表)

declare @MonthSeed table (MonthNumber int) 
insert into @MonthSeed values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)

-- assumes declared table "Reading" with fields ( Id int, [Date] datetime, MeterNo varchar(50), Consumption int )

select
    m.MeterNo,
    r.Date,
    calculatedConsumption = isnull(r.Consumption, -- read consumption
                                isnull((select max(r2.Consumption) Consumption from Reading r2 where datepart(month, r2.Date) = (m.MonthNumber - 1) and r2.MeterNo = m.MeterNo) * 1.15, -- previous consumption + 15%
                                    9999)) -- default consumption
from
    (select distinct
        MeterNo,
        MonthNumber
    from
        Reading, @MonthSeed) m
left join
    Reading r on r.MeterNo = m.MeterNo and datepart(month, r.Date) = m.monthNumber

编辑以下评论 - 添加缺失读数的示例

正如所评论的,需要在选择之前包含一个插入,insert into Reading (MeterNo, Date, Consumption)并使用reading表的左连接包括检查读取 id 是否为 null 即缺失where r.Id is null

我注意到这会在插入表时导致空日期条目reading。所以我在主子选择中包含了一个日期聚合Date = dateadd(month, monthnumber, @seeddate);修改了主选择以显示丢失条目的日期isnull(r.Date, m.Date),

一年前我已经计算出@SeedDate当月的 1 日,但您可能希望更早地传递。

declare @MonthSeed table (MonthNumber int) 
insert into @MonthSeed values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)

-- assumes declared table "Reading" with fields ( Id int, [Date] datetime, MeterNo varchar(50), Consumption int )

declare @SeedDate datetime = (select dateadd(month, datediff(month, 0, getdate())-12, 0)) -- this month, last year

insert into Reading (MeterNo, Date, Consumption)
select
    m.MeterNo,
    isnull(r.Date, m.Date),
    calculatedConsumption =
        isnull(r.Consumption, -- read consumption
        isnull(1.15 * (select max(r2.Consumption) Consumption 
                from Reading r2 
                where datepart(month, r2.Date) = (m.MonthNumber - 1) 
                and r2.MeterNo = m.MeterNo), -- previous consumption + 15%
        9999)) -- default consumption
from
    (select distinct
        MeterNo,
        MonthNumber,
        Date = dateadd(month, monthnumber, @seeddate)
    from
        Reading
    cross join
        @MonthSeed) m
left join
    Reading r on r.MeterNo = m.MeterNo and datepart(month, r.Date) = m.monthNumber
where
    r.Id is null

select * from Reading
于 2012-05-25T22:25:03.410 回答
0

(以下假设 SQL Server 2005 或更高版本。)

在这里四处搜寻,看看是否有任何有价值的东西:

declare @StartDate as Date = '2012-01-01'
declare @Now as Date = GetDate()
declare @DefaultConsumption as Int = 2000 -- KWh.

declare @MeterReadings as Table
  ( MeterReadingId Int Identity, ReadingDate Date, MeterNumber VarChar(10), Consumption Int )
insert into @MeterReadings ( ReadingDate, MeterNumber, Consumption ) values
  ( '2012-01-13', 'E154', 2710 ),
  ( '2012-01-19', 'BR549', 650 ),
  ( '2012-02-15', 'E154', 2970 ),
  ( '2012-02-19', 'BR549', 618 ),
  ( '2012-03-16', 'BR549', 758 ),
  ( '2012-04-11', 'E154', 2633 ),
  ( '2012-04-20', 'BR549', 691 )

; with Months ( Month ) as (
  select @StartDate as [Month]
  union all
  select DateAdd( mm, 1, Month )
    from Months
    where Month < @Now
  ),
MeterNumbers ( MeterNumber ) as (
  select distinct MeterNumber
    from @MeterReadings )
select M.Month, MN.MeterNumber,
  MR.MeterReadingId, MR.ReadingDate, MR.Consumption,
  Coalesce( MR.Consumption, @DefaultConsumption ) as [BillableConsumption],
  ( select Max( ReadingDate ) from @MeterReadings where MeterNumber = MN.MeterNumber and ReadingDate < M.Month ) as [PriorReadingDate],
  ( select Consumption from @MeterReadings where MeterNumber = MN.MeterNumber and ReadingDate =
  ( select Max( ReadingDate ) from @MeterReadings where MeterNumber = MN.MeterNumber and ReadingDate < M.Month ) ) as [PriorConsumption],
  ( select Consumption from @MeterReadings where MeterNumber = MN.MeterNumber and ReadingDate =
  ( select Max( ReadingDate ) from @MeterReadings where MeterNumber = MN.MeterNumber and ReadingDate < M.Month ) ) * 1.15 as [PriorConsumptionPlus15Percent]  
  from Months as M cross join
    MeterNumbers as MN left outer join
    @MeterReadings as MR on MR.MeterNumber = MN.MeterNumber and DateAdd( dd, 1 - DatePart( dd, MR.ReadingDate ), MR.ReadingDate ) = M.Month
  order by M.Month, MN.MeterNumber
于 2012-05-26T04:23:08.763 回答