1

我的桌子是:

Logtime              Energy
-------------------  --------
2013-02-22 15:30:00  36883.79
2013-02-21 17:30:00  36767.68
2013-02-20 17:30:00  35447.97
2013-02-19 17:30:00  34170.04
2013-02-18 15:30:00  32663.54
2013-02-15 17:30:00  31649.39
2013-02-13 17:30:00  29091.99
2013-02-12 17:30:00  27807.49

我需要得到两个 logtime 值之间的差异。

例如:

2013-02-18 15:30:00和之间的能量差2013-02-22 15:30:004220.25

我试过这样:

declare @energy varchar(30)
declare @min int
declare @max int
declare @result varchar(30)
select Logtime,Energy from PLENE_EnergySum order by Logtime desc
set @min=cast((select min(Energy) as [Energy] from PLENE_EnergySum where Logtime between
 '2013-02-22 15:30:00' and  '2013-02-18 15:30:00')as int)
set @max=cast((select max(Energy) as [Energy] from PLENE_EnergySum where Logtime between
'2013-02-22 15:30:00' and  '2013-02-18 15:30:00')as int)
set @result=@max-@min
insert into weekreport values('Fourth',@result) 

我在周报告表中得到空值

week    energy
------  ------
Fourth  NULL
4

1 回答 1

0

set @first=Cast(Year(getdate())as varchar(10))+'-'+Cast(Month(getdate())as varchar(10))+'-'+'01'

set @st=Cast(Year(getdate())as varchar(10))+'-'+Cast(Month(getdate())as varchar(10))+'-'+'08'

set @st1=Cast(Year(getdate())as varchar(10))+'-'+Cast(Month(getdate())as varchar(10))+'-'+'15'

set @st2=Cast(Year(getdate())as varchar(10))+'-'+Cast(Month(getdate())as varchar(10))+'-'+'22'

set @st3=Cast(Year(getdate())as varchar(10))+'-'+Cast(Month(getdate())as varchar(10))+'-'+'32'

set @min=(select min(Energy) as [Energy] from PLENE_EnergySum where Logtime>@first and Logtime<@st)

set @max=(select max(Energy) as [Energy] from PLENE_EnergySum where Logtime>@first and Logtime<@st)

set @result=@max-@min

insert into weekreport(week,Energy) values('First',@result)

Answer for the above table:

First 961.1

于 2013-02-27T12:03:13.703 回答