由于有了另一个用户的帮助,我才能够使用条件聚合来获取所需的数据点。现在,我需要将其实现到现有查询中,以获取日期范围(而非每个包)的SLA%。供参考的上一篇文章:Pull a DATEDIFF between Rows with Distinct value and WHERE Clause
当“ PackageTable”中的两个时间戳足以计算SLA的假设时,使用以下查询。因为我发现它们不是,所以我必须在另一个表(PackageTable_Audit)上运行查询,该表基本上在包从LifeCycleStatusId = 1(创建)移动到LifeCycleStatusId = 3(分配)到LifeCycleStatusId = 5时连续记录事件(已完成)。因此,SLA遵守百分比是指X秒钟内完成的软件包数量/总软件包数。由于我无法在子查询中使用简单的DATEDIFF,因此不得不使用聚合函数在行之间获取DATEDIFF,因此我不确定如何将其用于查询。
我已经使用条件聚合更新了旧查询,但是出现以下错误:
“无法对包含聚合或子查询的表达式执行聚合功能。”
查询:
-- VARIABLE DECLARATION AND INITIALIZATION
DECLARE @StartDate varchar(10);
DECLARE @EndDate varchar(10);
SET @StartDate = '2019-06-01';
SET @EndDate = '2019-06-31';
-- TABLE DECLARATION ##################################################
DECLARE @TABLE1 TABLE("No. Packages in SLA" INT, "Total Packages" INT, "SLA %" FLOAT)
--#####################################################################
-- WHAT GETS INSERTED INTO TABLE 1
INSERT INTO @TABLE1
SELECT
A.NUM, A.DENOM, CAST(A.NUM AS FLOAT)/A.DENOM*100
FROM
(
-- COLUMN SELECTION. TWO NUMBERS WILL REPRESENT A NUM AND A DENOM
SELECT
(SELECT SUM(CASE
WHEN
datediff(second, MAX(CASE WHEN LifeCycleStatusId = 2 THEN rowDateModified END),
MAX(CASE WHEN LifeCycleSTatusId = 5 THEN rowDateModified END)
) < 172800
THEN 1
ELSE 0
END) AS IN_SLA
FROM PackageTable WITH (nolock)
WHERE lifecyclestatusid = 5
AND rowDateCreated BETWEEN @StartDate AND @EndDate)
AS NUM,
(SELECT COUNT(PackageGuid) As No_Packages
FROM PackageTable WITH (nolock)
WHERE lifecyclestatusid = 5
AND rowDateCreated BETWEEN @StartDate AND @EndDate)
AS DENOM
) A
SELECT "No. Packages in SLA", "Total Packages", "SLA %"
FROM @TABLE1