1
我需要一个查询来评估列中最长的不间断系列后续“1”。对于表TEST(row_no number, fl_succ_exec number(1))
中的以下数据,查询结果应为“6”。用于查找值系列长度的SQL查询
行按row_no
排序。
ROW_NO FL_SUCC_EXEC
---------- ------------
1 1
2 1
3 1
4 0
5 1
6 1
7 1
8 1
9 1
10 1
11 0
12 1
13 1
14 1
15 1
我可以在PL/SQL做到这一点:
declare
temp_cnt pls_integer default 0;
total_cnt pls_integer default 0;
begin
for rec in (select row_no, fl_succ_exec from test order by row_no)
loop
if temp_cnt > total_cnt
then
total_cnt:=temp_cnt;
end if;
if rec.fl_succ_exec!=0
then
temp_cnt:=temp_cnt+rec.fl_succ_exec;
else
temp_cnt:=0;
end if;
end loop;
dbms_output.put_line(total_cnt);
end;
但我仍然希望SQL解决方案。有没有?