计算每次调用递归函数所花费的时间....在SQL
Calculate the time taken by a recursive function each time it is called.... in SQL
我有如下数据
id | Function_name | event | time
1 function_a start 12:01
2 function_a start 12:02
3 function_a start 12:03
4 function_a end 12:04
5 function_a start 12:05
6 function_a end 12:06
7 function_a end 12:07
8 function_a end 12:08
在上面的数据中,function_a被递归调用了4次,并记录了函数的开始和结束。我需要捕获每个函数调用所花费的时间。上述 table 的输出应如下所示。
Function_call_id | time_taken_for_function_call(seconds)
1 7
2 5
3 1
4 1
您可以枚举调用然后聚合。这枚举了开始和停止
select t.*,
(next_time - time) as diff
from (select t.*, lead(time) over (partition by grp order by id) as next_time
from (select t.*,
(sum(case when event = 'start' then 1 else -1 end) over (order by id) +
(case when event = 'end' then 1 else 0 end)
) as grp
from t
) t
) t
where event = 'start';
我有如下数据
id | Function_name | event | time
1 function_a start 12:01
2 function_a start 12:02
3 function_a start 12:03
4 function_a end 12:04
5 function_a start 12:05
6 function_a end 12:06
7 function_a end 12:07
8 function_a end 12:08
在上面的数据中,function_a被递归调用了4次,并记录了函数的开始和结束。我需要捕获每个函数调用所花费的时间。上述 table 的输出应如下所示。
Function_call_id | time_taken_for_function_call(seconds)
1 7
2 5
3 1
4 1
您可以枚举调用然后聚合。这枚举了开始和停止
select t.*,
(next_time - time) as diff
from (select t.*, lead(time) over (partition by grp order by id) as next_time
from (select t.*,
(sum(case when event = 'start' then 1 else -1 end) over (order by id) +
(case when event = 'end' then 1 else 0 end)
) as grp
from t
) t
) t
where event = 'start';