数据库中当前时间的数据减当天0点的值,算出今天的累计量。
如果现在时间为0点,则减去前一晚0点的值。
declare @newValue int
declare @oldValue int
declare @newTime datetime
declare @oldTime datetime
select @newTime= (select max(writedate)from test )
if @newTime= CONVERT(varchar(10), GETDATE(), 120)
begin
select @newTime = CONVERT(varchar(10), GETDATE(), 120)
select @oldTime= CONVERT(varchar(10), DATEADD(day, - 1, GETDATE()), 120)
end
else
begin
select @oldTime = CONVERT(varchar(10), GETDATE(), 120)
end
select @newValue = (select value1 from test where writedate=@newTime)
select @oldValue = (select value1 from test where writedate=@oldTime)
select @newValue-@oldValue as result
表的生成代码如下:
CREATE TABLE [dbo].[test](
[value1] [int] NULL,
[writeDate] [datetime] NOT NULL