如果我理解正确,您希望根据9/0/2的累积和定义组,然后聚合:
select equipment_type, machine_num,
min(datestamp), max(datestamp)
from (select t.*,
sum(case when status in (0, 9, 2) then 1 else 0 end) over (partition by equipment_type, machine_num order by datestamp) as grp
from t
) t
group by equipment_type, machine_num, grp;
这是一个db<>小提琴
老实说,您的示例结果是一个简单的聚合,它不遵循您根据状态指定的规则:
select equipment_type, machine_num,
min(datestamp), max(datestamp)
from t
group by equipment_type, machine_num;