如何从 table 中获取过滤选择记录下的计数百分比
How to get the percentage of count under filtered selection of records from a table
有一个架构 (Perf_Stat) 在一段时间内填充了性能统计数据:
Event Event_Ts Event_Stat
------------------------------------------
Entry 2021-02-15 19:30:10 Success
Update 2021-02-15 19:35:10 Success
Delete 2021-02-15 19:23:10 Failure
Update 2021-02-15 19:68:10 Success
Delete 2021-02-15 18:30:10 Failure
Update 2021-02-15 18:59:10 Success
and so no.....
我需要从这个 table 中获取失败的 Update 事件的百分比计数,无论何时执行。喜欢
在下一个 运行 上,它应该给我的结果是:
**23%** (Which denotes 23% of the total **Update** events that failed in the last hour)
我尝试过但失败的是:
SELECT COUNT
(
(
(SELECT COUNT(*) FROM Perf_Stat WHERE Event = 'Update' AND DATEDIFF(hour, Event_Ts, GETDATE() < 1) AND Event_Stat = 'Failure') * 100
) /
(
SELECT COUNT(*) FROM Perf_Stat WHERE Event = 'Update' AND DATEDIFF(hour, Event_Ts, GETDATE() < 1)
)
) from Perf_Stat;
但是,我收到聚合函数错误。请建议如何获得所需的结果。
I need to get the count in the percentage of failed Update events from this table for every last one hour whenever executed.
我会这样处理:
SELECT AVG(CASE WHEN Event = 'Update' THEN 1.0 ELSE 0 END)
FROM FROM Perf_Stat
WHERE DATEDIFF(hour, Event_Ts, GETDATE()) < 1;
我假设时间比较是您真正想要的 -- 最后一个完整小时。最后一个小时,我会使用:
WHERE Event_Ts >= DATEADD(hour, -1, GETDATE())
有一个架构 (Perf_Stat) 在一段时间内填充了性能统计数据:
Event Event_Ts Event_Stat
------------------------------------------
Entry 2021-02-15 19:30:10 Success
Update 2021-02-15 19:35:10 Success
Delete 2021-02-15 19:23:10 Failure
Update 2021-02-15 19:68:10 Success
Delete 2021-02-15 18:30:10 Failure
Update 2021-02-15 18:59:10 Success
and so no.....
我需要从这个 table 中获取失败的 Update 事件的百分比计数,无论何时执行。喜欢
在下一个 运行 上,它应该给我的结果是:
**23%** (Which denotes 23% of the total **Update** events that failed in the last hour)
我尝试过但失败的是:
SELECT COUNT
(
(
(SELECT COUNT(*) FROM Perf_Stat WHERE Event = 'Update' AND DATEDIFF(hour, Event_Ts, GETDATE() < 1) AND Event_Stat = 'Failure') * 100
) /
(
SELECT COUNT(*) FROM Perf_Stat WHERE Event = 'Update' AND DATEDIFF(hour, Event_Ts, GETDATE() < 1)
)
) from Perf_Stat;
但是,我收到聚合函数错误。请建议如何获得所需的结果。
I need to get the count in the percentage of failed Update events from this table for every last one hour whenever executed.
我会这样处理:
SELECT AVG(CASE WHEN Event = 'Update' THEN 1.0 ELSE 0 END)
FROM FROM Perf_Stat
WHERE DATEDIFF(hour, Event_Ts, GETDATE()) < 1;
我假设时间比较是您真正想要的 -- 最后一个完整小时。最后一个小时,我会使用:
WHERE Event_Ts >= DATEADD(hour, -1, GETDATE())