拥有一个模式(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.....
我需要得到失败的百分比计数更新此表中每隔一小时执行一次的事件。像
在下一次运行时,它应该给我的结果是:
**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;
但是,我得到聚合函数错误。请告诉我怎样才能得到想要的结果。
我需要从该表中获取最近一小时内每次执行的Update事件失败的百分比计数。
我会这样处理:
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())