从SSRS中的同一列中减去时间值

Substract time-values from same column in SSRS

我需要从同一列中减去时间值。我已经订购了需要相互减去的值。我将添加一张我的 table 的图片,以便您有一个大概的了解。现在我需要从数字 2 中减去数字 1,从数字 3 中减去数字 2,....(来自同一辆车)并将其放在名为 Consumption 的列中,例如。

作为 If 函数给我一个错误,我试图转到 case 函数。但现在我收到错误消息:“为变量赋值的 SELECT 语句不得与数据检索操作结合使用。问题代码是直到 @lastvalue = AGVLoggingrunstate.starttime 的 CASE 函数。我显然不能把这段代码放在数据检索代码中。那我该怎么做呢?我需要做一个存储过程吗?欢迎提示。

Declare @lastSN AS nvarchar, @lastValue int;  
SET @lastSN = 'AGV';
SET @lastValue = 0;

SELECT ROW_NUMBER() OVER (ORDER BY AgvLoggingRunstate.vehicle,AgvLoggingRunstate.starttime) AS Row,
  AgvLoggingRunstate.id,
  AgvLoggingRunstate.vehicle,
  AgvLoggingRunstate.node,
  AgvLoggingRunstate.runstate,
  AgvLoggingRunstate.orderclass,
  AgvLoggingRunstate.loaded,
  AgvLoggingRunstate.starttime,
  AgvLoggingRunstate.endtime,
  DATEDIFF(second,AgvLoggingRunstate.starttime,AgvLoggingRunstate.endtime) AS DiffDate,
Case 
WHEN @lastSN = AgvLoggingRunstate.vehicle Then AgvLoggingRunstate.starttime - @lastValue 
ELSE 0 
END AS Consumption,

@lastSN = AgvLoggingRunstate.vehicle,
@lastValue = AgvLoggingRunstate.starttime


FROM  AgvLoggingRunstate

Where AgvLoggingRunstate.starttime > @Startdate and   AgvLoggingRunstate.starttime < @Enddate and AgvLoggingRunstate.runstate = 'Battery'

Order by

AgvLoggingRunstate.vehicle,
AgvLoggingRunstate.starttime

回答并添加 order by 函数后的结果:

现在唯一错误的答案是在两天之间。我需要过滤掉这个。 (仍在阅读 COALESCELEFT JOIN、.. 因为答案对我来说还不完全清楚)

WITH Times (RowNumber, vehicle, starttime,endtime)
AS
(SELECT ROW_NUMBER() OVER (ORDER BY vehicle, starttime),vehicle, starttime,endtime FROM AgvLoggingRunState WHERE starttime > @Startdate and  starttime < @Enddate and AgvLoggingRunstate.runstate = 'Battery') 

SELECT CurrentTime.Vehicle,
             CurrentTime.StartTime,
             CurrentTime.EndTime, 
             NextTime.StartTime AS NextTime,
             COALESCE(DATEDIFF(SECOND,CurrentTime.StartTime,NextTime.StartTime),0)                      
             AS Seconds,
             COALESCE(DATEDIFF(SECOND,CurrentTime.StartTime,CurrentTime.EndTime),0) 
             AS SecondsDiffEnd
FROM Times CurrentTime
  LEFT OUTER JOIN Times NextTime
  ON CurrentTime.RowNumber +1  = NextTime.RowNumber
  AND CurrentTime.Vehicle = NextTime.Vehicle  

Order by StartTime

您正确地开始了行号功能。但是,您正在尝试对变量执行您需要对连接执行的操作。在下面的示例中,您将看到我如何使用您的行号创建 CTE,然后在查询中使用它,连接到下一条记录(只要车辆匹配)。 您需要为每辆车的最后一条记录确定正确答案。现在,它为零(请参阅 Coalesce 函数) 在 LEAD 的未来版本中,这将非常容易。好吧。

WITH Times (RowNumber, Vehicle, StartTime)
AS 
(SELECT ROW_NUMBER() OVER(ORDER BY Vehicle, StartTime),
 Vehicle, StartTime, NextTime FROM dbo.AvgLoggingRunState)

SELECT CurrentTime.Vehicle, 
       CurrentTime.StartTime, 
       NextTime.StartTime AS NextTime,
       COALESCE(DATEDIFF(SECOND,CurrentTime.StartTime,NextTime.StartTime),0)                     
          AS SecondsDiffNext,
      COALESCE(DATEDIFF(SECOND,CurrentTime.StartTime,CurrentTime.EndTime),0) 
          AS SecondsDiffEnd
FROM Times CurrentTime
   -- join to the next time - note that I join both on row number and
   -- on vehicles as I don't want to mix vehicles
  LEFT OUTER JOIN Times NextTime
  ON CurrentTime.RowNumber +1  = NextTime.RowNumber
  AND CurrentTime.Vehicle = NextTime.Vehicle