SQL 服务器:对部分空数据使用聚合
SQL Server : using aggregations on partially null data
我的一些代码有问题,我需要一些帮助。下面的附件是从 PowerApps 进入 SQL 数据库的 table:
+-----------------------+-----------------+-------------+-----------+------------+------+-----------+
| ID | ProductionOrder | Workstation | Status | Date | Pass | TotalTime |
+-----------------------+-----------------+-------------+-----------+------------+------+-----------+
| 60127429_1000_1_S035 | 108100 | S035 | Completed | 05/13/2020 | No | 121 |
| 60128523_1000_1_S120 | 884171 | S120 | Completed | 05/13/2020 | Yes | 18 |
| 60128523_13000_1_S080 | 884172 | S080 | Completed | 05/13/2020 | No | 26 |
| 60128523_13000_1_S090 | 884172 | S090 | Completed | 05/13/2020 | No | 19 |
| 60128523_13000_1_S120 | 884172 | S120 | Completed | 05/13/2020 | No | 16 |
| 60128523_28000_1_S070 | 884173 | S070 | Completed | 05/13/2020 | No | 10 |
| 60128523_28000_1_S080 | 884173 | S080 | Completed | 05/13/2020 | No | 16 |
| 60128523_28000_1_S090 | 884173 | S090 | Completed | 05/13/2020 | No | 21 |
| 60128523_28000_1_S120 | 884173 | S120 | Completed | 05/13/2020 | No | 17 |
| 60128523_47000_1_S080 | 884174 | S080 | Completed | 05/13/2020 | Yes | 10 |
| 60128523_47000_1_S090 | 884174 | S090 | Completed | 05/13/2020 | Yes | 50 |
| 60128523_47000_1_S120 | 884174 | S120 | Completed | 05/13/2020 | Yes | 19 |
| 60128569_6000_1_S080 | 884698 | S080 | Completed | 05/13/2020 | Yes | 9 |
| 60128569_6000_1_S090 | 884698 | S090 | Completed | 05/13/2020 | Yes | 47 |
| 60128569_6000_1_S120 | 884698 | S120 | Completed | 05/13/2020 | Yes | 54 |
| 60129071_1000_1_S070 | 105597 | S070 | Completed | 05/13/2020 | No | 137 |
| 60129071_1000_1_S080 | 105597 | S080 | Completed | 05/13/2020 | No | 12 |
| 60129071_1000_1_S090 | 105597 | S090 | Completed | 05/13/2020 | No | 21 |
| 60129071_23000_1_S070 | 105598 | S070 | Completed | 05/13/2020 | Yes | 50 |
| 60129071_23000_1_S080 | 105598 | S080 | Completed | 05/13/2020 | Yes | 12 |
| 60129071_23000_1_S090 | 105598 | S090 | Completed | 05/13/2020 | Yes | 29 |
| 60129071_23000_1_S120 | 105598 | S120 | Completed | 05/13/2020 | Yes | 20 |
| 60129071_23025_1_S020 | 105600 | S020 | Completed | 05/13/2020 | Yes | 21 |
| 60129151_2000_1_S090 | 104974 | S090 | Completed | 05/13/2020 | No | 26 |
| 60129151_2000_1_S120 | 104974 | S120 | Completed | 05/13/2020 | No | 24 |
+-----------------------+-----------------+-------------+-----------+------------+------+-----------+
接下来,我将创建一个视图,通过对每个工作站进行分组来获取一些指标。这是代码:
with cteNow AS
(
SELECT CASE WHEN 60 * DATEPART(HOUR, GETUTCDATE()) + DATEPART(MINUTE, GETUTCDATE()) > 60 * 21
THEN CONVERT(float,(16 - 7) * 60) --minutes in (4 PM - 7 AM) * 60 minutes/hour
ELSE
CONVERT(float,60 * DATEPART(HOUR, GETUTCDATE()) + DATEPART(MINUTE, GETUTCDATE()) - ((7 + 5) * 60))
END as MinutesToday
, CONVERT(varchar(50), DATEADD(HOUR, -5, GETUTCDATE()), 101) as TargetDate
)
SELECT dbs.Workstation,
100.0 - ISNULL(SUM(CASE WHEN dbs.Pass = 'Yes' AND Status = 'Completed' THEN 1 ELSE 0 END) / NULLIF(SUM(CASE WHEN dbs.Status = 'Completed' THEN 1 ELSE 0 END), 0), 0) AS RFT
FROM [Booms DBS] AS dbs LEFT JOIN [Booms Delay Entry] AS del ON dbs.ID = del.ID INNER JOIN cteNow AS N ON N.TargetDate = dbs.[Date]
GROUP BY dbs.Workstation, MinutesToday
这样做的目的是为每个站点测量 "Right First Time"。每当 Pass
列中出现 "Yes" 值时,它就是对 RFT 的命中。例如,如果给定站点中总共有十个构建,其中五个在 Pass
列中具有 "Yes",则为 50% RFT。
但是,这总是给我 RFT
列的结果“100”。我添加了所有这些 NULLIF
和 ISNULL
警告,因为有时 Pass
列可能为空,并且在某些日子里,可能根本没有任何数据,尤其是在早上。我假设这就是问题所在。我没有正确计算吗?如果我不包含那些空值检查函数,我想不出任何其他方法来避免出错或出现被零除错误。
提前致谢。
The point of this is to measure "Right First Time" for each station. Every time there is a "Yes" value in the Pass column, it is a hit to RFT. For example, if there is ten total builds in a given station and five of them have "Yes" in the Pass column, that is 50% RFT.
您似乎需要条件聚合。我不明白您现有查询中日期算法的目的,但这会按照您的描述进行:
select
workstation,
avg(case when statys = 'Completed' and pass = 'Yes' then 1.0 else 0 end) rft_rate
from dbs.workstation
group by workstation
这为每个 workstation
提供一条记录,数值介于 0 和 1 之间,表示在 pass
列中具有值 'Yes'
的记录的比率。[=15] =]
我的一些代码有问题,我需要一些帮助。下面的附件是从 PowerApps 进入 SQL 数据库的 table:
+-----------------------+-----------------+-------------+-----------+------------+------+-----------+
| ID | ProductionOrder | Workstation | Status | Date | Pass | TotalTime |
+-----------------------+-----------------+-------------+-----------+------------+------+-----------+
| 60127429_1000_1_S035 | 108100 | S035 | Completed | 05/13/2020 | No | 121 |
| 60128523_1000_1_S120 | 884171 | S120 | Completed | 05/13/2020 | Yes | 18 |
| 60128523_13000_1_S080 | 884172 | S080 | Completed | 05/13/2020 | No | 26 |
| 60128523_13000_1_S090 | 884172 | S090 | Completed | 05/13/2020 | No | 19 |
| 60128523_13000_1_S120 | 884172 | S120 | Completed | 05/13/2020 | No | 16 |
| 60128523_28000_1_S070 | 884173 | S070 | Completed | 05/13/2020 | No | 10 |
| 60128523_28000_1_S080 | 884173 | S080 | Completed | 05/13/2020 | No | 16 |
| 60128523_28000_1_S090 | 884173 | S090 | Completed | 05/13/2020 | No | 21 |
| 60128523_28000_1_S120 | 884173 | S120 | Completed | 05/13/2020 | No | 17 |
| 60128523_47000_1_S080 | 884174 | S080 | Completed | 05/13/2020 | Yes | 10 |
| 60128523_47000_1_S090 | 884174 | S090 | Completed | 05/13/2020 | Yes | 50 |
| 60128523_47000_1_S120 | 884174 | S120 | Completed | 05/13/2020 | Yes | 19 |
| 60128569_6000_1_S080 | 884698 | S080 | Completed | 05/13/2020 | Yes | 9 |
| 60128569_6000_1_S090 | 884698 | S090 | Completed | 05/13/2020 | Yes | 47 |
| 60128569_6000_1_S120 | 884698 | S120 | Completed | 05/13/2020 | Yes | 54 |
| 60129071_1000_1_S070 | 105597 | S070 | Completed | 05/13/2020 | No | 137 |
| 60129071_1000_1_S080 | 105597 | S080 | Completed | 05/13/2020 | No | 12 |
| 60129071_1000_1_S090 | 105597 | S090 | Completed | 05/13/2020 | No | 21 |
| 60129071_23000_1_S070 | 105598 | S070 | Completed | 05/13/2020 | Yes | 50 |
| 60129071_23000_1_S080 | 105598 | S080 | Completed | 05/13/2020 | Yes | 12 |
| 60129071_23000_1_S090 | 105598 | S090 | Completed | 05/13/2020 | Yes | 29 |
| 60129071_23000_1_S120 | 105598 | S120 | Completed | 05/13/2020 | Yes | 20 |
| 60129071_23025_1_S020 | 105600 | S020 | Completed | 05/13/2020 | Yes | 21 |
| 60129151_2000_1_S090 | 104974 | S090 | Completed | 05/13/2020 | No | 26 |
| 60129151_2000_1_S120 | 104974 | S120 | Completed | 05/13/2020 | No | 24 |
+-----------------------+-----------------+-------------+-----------+------------+------+-----------+
接下来,我将创建一个视图,通过对每个工作站进行分组来获取一些指标。这是代码:
with cteNow AS
(
SELECT CASE WHEN 60 * DATEPART(HOUR, GETUTCDATE()) + DATEPART(MINUTE, GETUTCDATE()) > 60 * 21
THEN CONVERT(float,(16 - 7) * 60) --minutes in (4 PM - 7 AM) * 60 minutes/hour
ELSE
CONVERT(float,60 * DATEPART(HOUR, GETUTCDATE()) + DATEPART(MINUTE, GETUTCDATE()) - ((7 + 5) * 60))
END as MinutesToday
, CONVERT(varchar(50), DATEADD(HOUR, -5, GETUTCDATE()), 101) as TargetDate
)
SELECT dbs.Workstation,
100.0 - ISNULL(SUM(CASE WHEN dbs.Pass = 'Yes' AND Status = 'Completed' THEN 1 ELSE 0 END) / NULLIF(SUM(CASE WHEN dbs.Status = 'Completed' THEN 1 ELSE 0 END), 0), 0) AS RFT
FROM [Booms DBS] AS dbs LEFT JOIN [Booms Delay Entry] AS del ON dbs.ID = del.ID INNER JOIN cteNow AS N ON N.TargetDate = dbs.[Date]
GROUP BY dbs.Workstation, MinutesToday
这样做的目的是为每个站点测量 "Right First Time"。每当 Pass
列中出现 "Yes" 值时,它就是对 RFT 的命中。例如,如果给定站点中总共有十个构建,其中五个在 Pass
列中具有 "Yes",则为 50% RFT。
但是,这总是给我 RFT
列的结果“100”。我添加了所有这些 NULLIF
和 ISNULL
警告,因为有时 Pass
列可能为空,并且在某些日子里,可能根本没有任何数据,尤其是在早上。我假设这就是问题所在。我没有正确计算吗?如果我不包含那些空值检查函数,我想不出任何其他方法来避免出错或出现被零除错误。
提前致谢。
The point of this is to measure "Right First Time" for each station. Every time there is a "Yes" value in the Pass column, it is a hit to RFT. For example, if there is ten total builds in a given station and five of them have "Yes" in the Pass column, that is 50% RFT.
您似乎需要条件聚合。我不明白您现有查询中日期算法的目的,但这会按照您的描述进行:
select
workstation,
avg(case when statys = 'Completed' and pass = 'Yes' then 1.0 else 0 end) rft_rate
from dbs.workstation
group by workstation
这为每个 workstation
提供一条记录,数值介于 0 和 1 之间,表示在 pass
列中具有值 'Yes'
的记录的比率。[=15] =]