HHMMSS 中的 SSRS 总时间

SSRS Total Time in HHMMSS

从上图中,我使用以下表达式对列 HHMMSS is SSRS 求和:

    =CStr(sum(CInt(split(Fields!HHMMSS.Value,":")(0)))
     +sum(CInt(split(Fields!HHMMSS.Value,":")(1))))
       &":"& CStr(sum(CInt(split(Fields!HHMMSS.Value,":")(1)))
      mod 60+sum(CInt(split(Fields!HHMMSS.Value,":")(2)))) 
      &":"& CStr(sum(CInt(split(Fields!HHMMSS.Value,":")(2))) mod 60)

如果列中没有空白,它工作正常,但当没有值或空白时会出错。我该如何克服这个问题? 报告的查询是:

    DECLARE @Table TABLE 
    (
          RowID       INT IDENTITY(1,1) PRIMARY KEY
 , EmpID       INT NOT NULL
 , StartTime   DATETIME NOT NULL DEFAULT('1900/00/00')
 , FinishTime  DATETIME NOT NULL DEFAULT('1900/00/00')
 , JobID       INT NULL
 , IdleTime    INT 
    )

    INSERT INTO @Table(EmpID,StartTime,FinishTime,JobID,IdleTime)
    VALUES (100,'2011-09-08 09:00:03.410','2011-09-08 09:55:18.153',12,1)
         , (100,'2011-09-08 11:55:03.810','2011-09-08 12:30:18.153',12,-1)
         , (101,'2012-10-17 09:19:52.637','2012-10-17 17:19:52.637',15,1)
     , (101,'2012-10-17 12:30:52.637','2012-10-17 13:25:52.637',15,-1)
     , (103,'2012-10-17 09:00:03.410','2012-10-17 16:19:52.637',20,1)
     , (103,'2012-10-17 13:00:03.410','2012-10-17 13:49:52.637',20,-1)
     , (104,'2012-10-17 09:00:03.410','2012-10-17 15:19:52.637',54,1)
     , (104,'2012-10-17 11:00:03.410','2012-10-17 13:19:52.637',54,-1)
     , (105,'2012-10-16 09:00:03.410','2012-10-17 18:19:52.637',56,1)
     , (105,'2012-10-17 13:00:03.410','2012-10-17 14:19:52.637',56,-1)
     , (106,'2012-10-10 09:00:03.310','2012-10-17 15:19:52.637',10,1)
     , (106,'2012-10-17 10:00:03.410','2012-10-17 10:34:52.637',10,-1)
     , (106,'2012-10-17 13:00:03.410','2012-10-17 13:35:52.637',10,-1)
     , (107,'2012-10-17 09:00:03.410','2012-10-17 15:19:52.637',17,1)
     , (108,'2012-10-17 09:00:03.410','2012-10-17 15:19:52.637',19,1)
     , (109,'2012-10-17 09:00:03.410','2012-10-17 18:19:52.637',11,1)
     , (109,'2012-10-17 10:00:03.410','2012-10-17 10:19:52.637',11,-1)
     , (109,'2012-10-17 12:00:03.410','2012-10-17 12:20:52.637',11,-1)
     , (109,'2012-10-18 14:00:03.410','2012-10-18 14:20:08.677',11,-1)
     , (110,'2012-10-17 09:00:03.410','2012-10-17 15:19:52.637',20,1)
         , (101,'2012-10-18 09:19:52.637','2012-10-18 17:19:52.637',15,1)
     , (101,'2012-10-18 12:30:52.637','2012-10-18 13:25:52.637',15,-1)
     , (103,'2012-10-18 09:00:03.410','2012-10-18 16:19:52.637',20,1)
     , (103,'2012-10-18 13:00:03.410','2012-10-18 13:49:52.637',20,-1)
     , (104,'2012-10-18 09:00:03.410','2012-10-18 15:19:52.637',54,1)
     , (104,'2012-10-18 11:00:03.410','2012-10-18 13:19:52.637',54,-1)
     , (105,'2012-10-18 09:00:03.410','2012-10-18 18:19:52.637',56,1)
     , (105,'2012-10-18 13:00:03.410','2012-10-18 14:19:52.637',56,-1)
     , (106,'2012-10-18 09:00:03.310','2012-10-18 15:19:52.637',100,1)
     , (106,'2012-10-18 10:00:03.410','2012-10-18 10:34:52.637',100,-1)
     , (106,'2012-10-18 13:00:03.410','2012-10-18 13:35:52.637',10,-1)
     , (107,'2012-10-18 09:00:03.410','2012-10-18 15:19:52.637',17,1)
     , (108,'2012-10-18 09:00:03.410','2012-10-18 15:19:52.637',19,1)
     , (109,'2012-10-18 09:00:03.410','2012-10-18 18:19:52.637',133,1)
     , (109,'2012-10-18 10:00:03.410','2012-10-18 10:19:52.637',133,-1)
     , (109,'2012-10-18 12:00:03.410','2012-10-18 12:20:52.637',133,-1)
     , (109,'2012-10-18 14:00:03.410','2012-10-18 14:20:08.677',133,-1)
     , (110,'2012-10-18 09:00:03.410','2012-10-18 15:19:52.637',31,1)

     SELECT EmpID
           , CONVERT(VARCHAR(10), StartTime, 103) AS [Date] 
       , CONVERT(VARCHAR(5), StartTime, 108) AS [Time]
       , CONVERT(VARCHAR(10), FinishTime, 103) AS Date_Off 
       , CONVERT(VARCHAR(5), FinishTime, 108) AS Time_Off
       , CASE WHEN IdleTime = -1 THEN '' 
     ELSE CONVERT(VARCHAR(10)
           , DATEADD(SECOND, DATEDIFF(SECOND,StartTime,FinishTime),0), 108)END AS HHMMSS
      , CASE WHEN IdleTime = -1 THEN CONVERT(VARCHAR(10), DATEADD(SECOND, DATEDIFF(SECOND,StartTime,FinishTime),0), 108)
    ELSE '' END AS TrainingTime
    FROM @Table

尝试在下面添加 IIF 语句:

=iif(IsNothing(Fields!HHMMSS.Value) OR Fields!HHMMSS.Value = "", "",
 CStr(sum(CInt(split(Fields!HHMMSS.Value,":")(0)))
 +sum(CInt(split(Fields!HHMMSS.Value,":")(1))))
   &":"& CStr(sum(CInt(split(Fields!HHMMSS.Value,":")(1)))
  mod 60+sum(CInt(split(Fields!HHMMSS.Value,":")(2)))) 
  &":"& CStr(sum(CInt(split(Fields!HHMMSS.Value,":")(2))) mod 60)
)

更新

=iif(Len(Fields!HHMMSS.Value) <= 0, "",
 CStr(sum(CInt(split(Fields!HHMMSS.Value,":")(0)))
 +sum(CInt(split(Fields!HHMMSS.Value,":")(1))))
   &":"& CStr(sum(CInt(split(Fields!HHMMSS.Value,":")(1)))
  mod 60+sum(CInt(split(Fields!HHMMSS.Value,":")(2)))) 
  &":"& CStr(sum(CInt(split(Fields!HHMMSS.Value,":")(2))) mod 60)
)

更新 2

=iif(Len(Fields!HHMMSS.Value) <= 0, "",
 CStr(sum(CInt(split(iif(Len(Fields!HHMMSS.Value) <= 0, "", Fields!HHMMSS.Value),":")(0)))
 +sum(CInt(split(iif(Len(Fields!HHMMSS.Value) <= 0, "", Fields!HHMMSS.Value),":")(1))))
   &":"& CStr(sum(CInt(split(iif(Len(Fields!HHMMSS.Value) <= 0, "", Fields!HHMMSS.Value),":")(1)))
  mod 60+sum(CInt(split(iif(Len(Fields!HHMMSS.Value) <= 0, "", Fields!HHMMSS.Value),":")(2)))) 
  &":"& CStr(sum(CInt(split(iif(Len(Fields!HHMMSS.Value) <= 0, "", Fields!HHMMSS.Value),":")(2))) mod 60)
)

您得到 #error 是因为您试图用整数添加 varchars (blank/nulls)。

而不是空白或 NULLHH:MM:SS 列设为 0:0:0(或 00:00:00)。如果您不想在报告中显示,您可以使用文本框中的隐藏表达式在报告级别隐藏它。通过此更改,SSRS 将不需要将 varchars 与数字混合,您将不会得到#error。

处理此问题的其他方法是在 SQL 查询本身中处理它。

SELECT EmpID, [Date], [Time],
CASE WHEN ColumnA = 'Training' THEN '00:00:00' ELSE CONVERT(VARCHAR(10), DATEADD(SECOND, DATEDIFF(SECOND,StartTime,FinishTime),0), 108) END AS HHMMSS
,
SUM(IIF (ColumnA='Training',0,DATEDIFF(SECOND, StartTime,FinishTime))) OVER  (PARTITION BY EmpID, [Date]) AS TotalTime
,CONVERT(VARCHAR(10),
DATEADD(SECOND,
    SUM(IIF (ColumnA='Training',0,DATEDIFF(SECOND, StartTime,FinishTime))) OVER  (PARTITION BY EmpID, [Date])
, 108) AS TotalTimeHHMMSS
    FROM ....
    WHERE ....

分区列取决于您如何根据 empid, jobid 和其他字段对数据进行分区。 现在你有了以秒为单位的总时间,你可以在 SSRS 或查询中转换它。