使用游标检查T-sql中一年中每个月的值

时间:2012-05-25 21:13:21

标签: tsql

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

光标需要检查每个仪表是否有当前年份的读数。对于缺少读数的米,增加估计值,使得每日消费量与前一时期的每日消费量相加,加上15%。在之前的任何时期都没有使用上述Kwh值。

2 个答案:

答案 0 :(得分: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

编辑后的评论 - 添加缺失读物的示例

由于注释需要在select 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计算为一年前当月的第一个,但您可能希望提前传递。

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

答案 1 :(得分: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