我在表格列中有以下值。表格中有两列。另一列按降序排列不同的日期。
3
4
3
21
4
4
-1
3
21
-1
4
4
8
3
3
-1
21
-1
4
我只需要图表中的高峰,输出中有圆圈
4
21
21
8
21
4
答案 0 :(得分:2)
因此峰值定义为前一个值,下一个值小于当前值,您可以使用LAG()和LEAD()函数检索上一个值。
您确实需要其他一些列(例如my_date)来定义行的顺序,然后您可以:
select my_date,
value
from (select value,
lag(value ) over (order by my_date) lag_value,
lead(value) over (order by my_date) lead_value
from my_table)
where value > coalesce(lag_value , value - 1) and
value > coalesce(lead_value, value - 1);
这不允许出现“双峰”,例如:
1,
15,
15,
4
......需要更复杂的逻辑。
答案 1 :(得分:2)
为了完整性,行模式匹配示例:
WITH source_data(datetime, value) AS (
SELECT DATE '2015-01-01', 3 FROM DUAL UNION ALL
SELECT DATE '2015-01-02', 4 FROM DUAL UNION ALL
SELECT DATE '2015-01-03', 3 FROM DUAL UNION ALL
SELECT DATE '2015-01-04', 21 FROM DUAL UNION ALL
SELECT DATE '2015-01-05', 4 FROM DUAL UNION ALL
SELECT DATE '2015-01-06', 4 FROM DUAL UNION ALL
SELECT DATE '2015-01-07', -1 FROM DUAL UNION ALL
SELECT DATE '2015-01-08', 3 FROM DUAL UNION ALL
SELECT DATE '2015-01-09', 21 FROM DUAL UNION ALL
SELECT DATE '2015-01-10', -1 FROM DUAL UNION ALL
SELECT DATE '2015-01-11', 4 FROM DUAL UNION ALL
SELECT DATE '2015-01-12', 4 FROM DUAL UNION ALL
SELECT DATE '2015-01-13', 8 FROM DUAL UNION ALL
SELECT DATE '2015-01-14', 3 FROM DUAL UNION ALL
SELECT DATE '2015-01-15', 3 FROM DUAL UNION ALL
SELECT DATE '2015-01-16', -1 FROM DUAL UNION ALL
SELECT DATE '2015-01-17', 21 FROM DUAL UNION ALL
SELECT DATE '2015-01-18', -1 FROM DUAL UNION ALL
SELECT DATE '2015-01-19', 4 FROM DUAL
)
SELECT *
FROM
source_data MATCH_RECOGNIZE (
ORDER BY datetime
MEASURES
LAST(UP.datetime) AS datetime,
LAST(UP.value) AS value
ONE ROW PER MATCH
PATTERN ((UP DOWN) | UP$)
DEFINE
DOWN AS DOWN.value < PREV(DOWN.value),
UP AS UP.value > PREV(UP.value)
)
ORDER BY
datetime
答案 2 :(得分:1)
Oracle 12c中有一种更复杂的方法,即使用模式匹配SQL。
http://docs.oracle.com/database/121/DWHSG/pattern.htm#DWHSG8966
对于这样的情况来说会有点矫枉过正,但如果你需要更复杂的模式匹配,比如W形图案,那么值得研究。