SELECT *
FROM table_name
MATCH_RECOGNIZE(
ORDER BY shipment_date
MEASURES
SUM(qty) AS qty,
LAST(shipment_date) AS shipment_date
PATTERN (first_day within_two_days*)
DEFINE
within_two_days AS shipment_date <= FIRST(shipment_date) + INTERVAL '2' DAY
)
其中,对于样本数据:
CREATE TABLE table_name (Qty, shipment_date) AS
SELECT 10, DATE '2023-09-20' FROM DUAL UNION ALL
SELECT 8, DATE '2023-09-19' FROM DUAL UNION ALL
SELECT 3, DATE '2023-09-18' FROM DUAL UNION ALL
SELECT 5, DATE '2023-09-16' FROM DUAL UNION ALL
SELECT 2, DATE '2023-09-13' FROM DUAL;
1条答案
按热度按时间emeijp431#
在Oracle 12中,您可以使用
MATCH_RECOGNIZE
进行逐行模式匹配:其中,对于样本数据:
输出:
| 数量|装运日期|
| --|--|
| 2 |2023-09-13 00:00:00|
| 8 |2023-09-18 00:00:00|
| 18 |2023-09-20 00:00:00:00|
fiddle