select a.time ,a.sum - b.sum sum,a.time,b.time from (select @arownum:=@arownum+1 rownum,sum ,time from summary_hour,(select @arownum:=0) t where energy_type= 1 order by time) a, (select @brownum:=@brownum+1 rownum ,sum,time from summary_hour,(select @brownum:=1) t where energy_type= 1 ORDEr by time) b where a.rownum = b.rownum;