sql相邻两条记录差值计算 初稿

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[query_all_steamdata]
@userCode varchar(255),
@startTime datetime,
@endTime datetime
as
select 
(dbo.UsersInfo.User_name + '(' + c.User_code + ')') AS [UserName], 
CONVERT(CHAR(19), c.Check_time, 20) as strCreateTime,
CONVERT(CHAR(19), c.Check_time, 20) as strCheckTime,
CAST(Avg(c.Temp_value) / 100.0 AS decimal(9, 2)) AS [Temp], 
CAST(Avg(c.Press_value) / 100.0 AS decimal(9, 2)) AS [Press],  CAST(Avg(c.freq_value) / 1000.0 AS decimal(9, 3)) AS [Freq],  
CAST(Avg(c.dP_value) / 1000.0 AS decimal(9, 3)) AS [dP],  CAST(Avg(c.Quantity_value) / 1000.0 AS decimal(9, 3)) AS [Quantity],  
CAST(Max(c.TQuantity_value) / 100.0 AS decimal(11, 0))AS [TQuantity],  


CAST((Max(c.TQuantity_value)-
(select top 1 a.TQuantity_value 
 FROM 
UseSteamAllData as a
, UsersInfo as b  
WHERE (a.User_code = b.User_code) 
AND (a.User_code = c.User_code) 
and  (a.check_time= @startTime) AND (a.Check_time <=@endTime)) order by a.check_time desc
))/100.0 

AS decimal(9, 2)

)AS [UQ] 


from UseSteamAllData  c,
UsersInfo
where
c.User_code = UsersInfo.User_code
AND 
c.User_code=@userCode
AND 
c.Check_time >=@startTime 
AND 
c.Check_time <=@endTime
GROUP BY c.User_code, dbo.UsersInfo.User_name,c.Check_time



你可能感兴趣的:(sql相邻两条记录差值计算 初稿)