sql - 如何从表中找到回归计数?
问题描述
我得到了一项特殊任务,要求我从表格中计算回归测试的数量。表是这样的:
|test |status|
|test1| pass |
|test2| fail |
|test3| fail |
|test4| fail |
|test5| pass |
|test6| fail |
...
回归计数的定义是当存在从通过到失败的过渡时,回归计数++。所以上表的回归计数将是 2。现在我不知道如何使用 T-SQL 完成该任务。是否可以使用 T-SQL 来处理,还是需要使用其他方法,例如编写 Python 脚本?
解决方案
SQL 表表示无序集。因此,要回答您的问题,我需要假设存在某种排序列。
使用这样的列,只需使用lead()
or lag()
:
select count(*)
from (select t.*, lead(status) over (order by <ordering column>) as next_status
from t
) t
where status = 'pass' and next_status = 'fail';
在您的数据中,排序列实际上可能是test
,但这并不清楚。
推荐阅读
- postgresql - golang-migrate - 如果数据库有更新的迁移版本,则跳过迁移
- python - 为什么算法返回 ['NoneType' is not iterable] 错误
- bash - 脚本选项采用通配符
- iframe - 如何强制 iframe 中的提交按钮在父窗口中打开
- python - 无法在 jupyter 笔记本中注册/访问/使用 conda 虚拟环境
- docker - docker绑定一个端口,但是绑定另一个端口,服务访问失败
- electron - 如何在 Electron 中制作一个透明的加载 GIF 窗口?
- android - 是否可以编写脚本将 PWA 应用程序添加到主屏幕?
- python - Python断言列表中的所有元素不是无
- html - 如何使用图像替换 Navbar.brand?