首页 > 解决方案 > 计算Oracle中的极值数

问题描述

我在 Oracle 表中有 481 个整数。它们的值随机变化,它可以表示为一个函数,其中 x 从 1 变为 481,y 从 0 变为 15。如何计算这些数字的所有局部最大值和最小值点(极值)的数量?

标签: sqloracle

解决方案


假设没有关系,您可以使用lead()and lag()

select sum(case when y > prev_y and y > next_y then 1 else 0 end) as local_maxima,
       sum(case when y < prev_y and y < next_y then 1 else 0 end) as local_minima
from (select t.*,
             lag(y) over (order by x) as prev_y,
             lead(y) over (order by x) as next_y
      from t
     ) t

推荐阅读