首页 > 解决方案 > 如何从表中找到回归计数?

问题描述

我得到了一项特殊任务,要求我从表格中计算回归测试的数量。表是这样的:

|test |status|
|test1| pass |
|test2| fail |
|test3| fail |
|test4| fail |
|test5| pass |
|test6| fail |
...

回归计数的定义是当存在从通过到失败的过渡时,回归计数++。所以上表的回归计数将是 2。现在我不知道如何使用 T-SQL 完成该任务。是否可以使用 T-SQL 来处理,还是需要使用其他方法,例如编写 Python 脚本?

标签: sqlsql-servertsql

解决方案


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,但这并不清楚。


推荐阅读