存储过程之日周月统计
CREATE proc energy_data
@id int
as
begin
declare @maxDL decimal , @minDayDL decimal, @minWeekDL decimal , @minMonthDL decimal,@DayDL decimal
-- get the maxDL
select @maxDL = max(convert(decimal,dianliang)) from data where datatime >= convert(nvarchar(20),getdate(),101) and id=@id
--get the minDayDL
select @minDayDL = min(dianliang) from data where datatime>convert(nvarchar(20),getdate(),101) and id=@id
--get the minWeekDL
select @minWeekDL = min(dianliang) from data where datatime>convert(nvarchar(20),dateadd(day,-7,getdate()),101) and id=@id
--get the minMonthDL
select @minMonthDL = min(dianliang) from data where datatime>convert(nvarchar(20),dateadd(day,-30,getdate()),101) and id=@id
begin
set @DayDL=@maxDL -@minDayDL
if @maxDL=null
begin
select @maxDL=max(convert(decimal,dianliang)) from data where id=@id
set @DayDL=0
end
select @DayDL as DayDL, @maxDL - @minWeekDL as WeekDL,@maxDL - @minMonthDL as MonthDL
end
end
GO
功能:
查询 统计日,周,月的数据