如何获取基于ID的计数和总和

时间:2019-07-13 19:11:39

标签: sql-server datetime join count average

我有一张叫大厅的桌子

QueueID FkBranch    IsActive    Status  AddedLocalTime          CompletedTime           FkAssistTypeID
553279  16              1           5   7/12/2019 20:06         7/12/2019  21:10                2
553278  16              1           5   7/12/2019 20:07         7/12/2019  21:11                1
553277  16              1           5   7/12/2019 20:08         7/12/2019  21:10                1
553276  16              1           5   7/12/2019 20:09         7/12/2019  21:11            1
553275  16              1           5   7/13/2019 20:10         7/13/2019  21:10            2
553274  16              1           5   7/13/2019 20:11         7/13/2019  21:11            2
553278  17              1           5   7/14/2019 20:07         7/14/2019  21:11                1
553277  17              1           5   7/14/2019 20:08         7/14/2019  21:10                1
553276  18              1           5   7/14/2019 20:09         7/14/2019  21:11            2
553275  18              1           5   7/15/2019 20:10         7/15/2019  21:10            2
553274  18              1           5   7/15/2019 20:11         7/15/2019  21:11            2

分支表及其数据如下

BranchID    BranchName IsActive
16          Delhi        1
17          Karnataka    1
18          Telangana    1

现在,我需要获取FkAssistTypeID之间每个位置的AddedLocalTime个计数,还需要对AddedLocalTimeCompletedTime的时差求和。

我有一个函数来获取两个日期的时间差,如下所示

dbo.fnTimetoSeconds(AddedLocalTime, CompletedTime, NULL)
CREATE FUNCTION [dbo].[fnTimetoSeconds]
(
    @dateOne DATETIME,@dateTwo DATETIME,@dateToConvert DATETIME
)
RETURNS INT
AS
BEGIN
    DECLARE @date DATETIME
    DECLARE @retValue INT
    IF(@dateToConvert IS NULL)
    BEGIN
        SET @dateToConvert = CASE WHEN(@dateTwo>@dateOne) THEN @dateTwo-@dateOne ELSE @dateOne-@dateTwo END
    END
    SET @date = DATEADD(D, 0, DATEDIFF(D, 0, @dateToConvert))
    IF(DATEPART(yy,@dateToConvert) = 1900)
    BEGIN
        SET @retValue = DATEDIFF(s,@date,@dateToConvert) + CASE WHEN DATEDIFF(D, 0, @dateToConvert) > 0 THEN DATEDIFF(D, 0, @dateToConvert) ELSE 0 END * 3600 * 24
    END
    ELSE
    BEGIN
        SET @retValue = DATEDIFF(s,@date,@dateToConvert)
    END
    RETURN @retValue
END

我的预期输出是 *请注意,此平均值列需要计算,假设

FkAssistTypeID = 1AddedLocalTime在7/12/2019和7/14/2019之间时,通过传递该行的AddedLocalTimeCompletedTimefnTimetoSeconds时间差,取每个时间差的总和并除以计数。

enter image description here

我需要将以上输出添加到临时表中。我该怎么办?

我刚刚尝试过,但这不是预期的

select 
  b.BranchId AS ID, 
  b.BranchName,
  count(case l.FkAssistTypeId when 1 then 1 end) as CountOf1,
  SUM(CASE WHEN (l.FkAssistTypeId = 1) THEN COALESCE((dbo.fnTimetoSeconds(CompletedTime, AssistedTime, NULL)),0) ELSE 0 END) AS Average 
from Branch b left join Lobby l
on b.BranchId = l.FkBranchId
where l.IsActive = 1 AND b.IsTestBranch = 0 AND CAST(l.AddedLocalTime as DATE) = '2019-07-12'
group by b.BranchId, b.BranchName

2 个答案:

答案 0 :(得分:0)

这样的事情怎么样:

SELECT Vals.ID, 
       Vals.BranchName , 
       Vals.CountOf1, 
       CASE WHEN (Vals.CountOf1 = 0) THEN 0 ELSE Vals.mySum/Vals.CountOf1 END as AveSecs
INTO tmpTbl 
FROM
  (SELECT 
    b.BranchId AS ID, 
    b.BranchName,
    count(l.FkAssistTypeI) as CountOf1,
    SUM(DATEDIFF(second, CompletedTime, AssistedTime)) AS mySum 
  FROM Branch b 
  LEFT OUTER JOIN Lobby l
  ON b.BranchId = l.FkBranchId
  WHERE l.IsActive = 1 
  AND b.IsTestBranch = 0 
  AND l.FkAssistTypeI = 1 
  AND l.AddedLocalTime >= '2019/07/12'
  AND l.AddedLocalTime < DATEADD(day, 1, '2019/07/15')
  GROUP BY b.BranchId, b.BranchName) as Vals

查询首先获取2019/07/12 00:00到2019/07/15 00:00(不包括)之间的计数和总和。然后,在计算平均值的同时,将其插入到临时表(如您所述)中。请注意,由于我尚未运行它,因此需要对其进行测试并进行一些调整。另外,我只是使用date diff计算以秒为单位的时间,但是如果需要,您可以使用函数。大多数时候,它们应该得出相同的值。

答案 1 :(得分:0)

不确定使用标量函数获取日期差的原因,即使使用当前逻辑,也可以在查询中执行此操作,而无需该函数。

SELECT 
    b.BranchID
,   b.BranchName
,   ISNULL(l.CountOF1,0) CountOF1
,   ISNULL(l.Average ,0) Average 
FROM 
    @Branch b
LEFT JOIN (
    SELECT
      FkBranch
    , IsActive
    , COUNT(*) CountOF1
    , AVG(DATEDIFF(SECOND, AddedLocalTime, CompletedTime)) Average 
    FROM @lobby l
    WHERE 
        FkAssistTypeID = 1
    AND AddedLocalTime BETWEEN '7/12/2019 00:00:00' AND '7/14/2019 23:59:59'
    GROUP BY FkBranch, IsActive
) l ON l.FkBranch = b.BranchID AND l.IsActive = b.IsActive
WHERE 
    b.IsActive = 1