Oracle 10g, 11g, MySQL, SQL SERVER (不支持 MATCH RECOGNIZE ) 如何将两个数据行警告关闭电源和电源合并为一个
Posted
技术标签:
【中文标题】Oracle 10g, 11g, MySQL, SQL SERVER (不支持 MATCH RECOGNIZE ) 如何将两个数据行警告关闭电源和电源合并为一个【英文标题】:How to merge two data row warning off power and on power into one in Oracle 10g, 11g , MySQL , SQL SERVER (no support MATCH RECOGNIZE ) 【发布时间】:2019-03-23 21:25:17 【问题描述】:https://drive.google.com/file/d/1yakmTD5M2MEINdBEg2ckUTb3vw7YPFTO/view?usp=sharing
大家好,
我已经说过链接链接的给定问题,并且可以通过支持模式匹配的 SQL 解决方案或 Wernfried Domscheit 的 ORACLE 12C 来解决该问题:
CREATE TABLE ALERT_EVENT (MEASUREMENT_POINT VARCHAR2(10), TIME_ALERT DATE, STATUS VARCHAR2(10));
INSERT INTO ALERT_EVENT VALUES('PE01', to_date('04/10/2018 00:00:00','dd/mm/yyyy hh24:mi:ss'), 'POWER ON');
INSERT INTO ALERT_EVENT VALUES('PE01', to_date('03/10/2018 00:00:00','dd/mm/yyyy hh24:mi:ss'), 'POWER OFF');
INSERT INTO ALERT_EVENT VALUES('PE01', to_date('02/10/2018 00:00:00','dd/mm/yyyy hh24:mi:ss'), 'POWER ON');
INSERT INTO ALERT_EVENT VALUES('PE01', to_date('01/10/2018 00:00:00','dd/mm/yyyy hh24:mi:ss'), 'POWER OFF');
INSERT INTO ALERT_EVENT VALUES('PE02', to_date('03/10/2018 00:00:00','dd/mm/yyyy hh24:mi:ss'), 'POWER OFF');
INSERT INTO ALERT_EVENT VALUES('PE02', to_date('02/10/2018 23:00:00','dd/mm/yyyy hh24:mi:ss'), 'POWER ON');
INSERT INTO ALERT_EVENT VALUES('PE02', to_date('02/10/2018 22:00:00','dd/mm/yyyy hh24:mi:ss'), 'POWER OFF');
INSERT INTO ALERT_EVENT VALUES('PE03', to_date('04/10/2018 10:00:00','dd/mm/yyyy hh24:mi:ss'), 'POWER OFF');
INSERT INTO ALERT_EVENT VALUES('PE03', to_date('02/10/2018 23:00:00','dd/mm/yyyy hh24:mi:ss'), 'POWER ON');
INSERT INTO ALERT_EVENT VALUES('PE03', to_date('02/10/2018 22:00:00','dd/mm/yyyy hh24:mi:ss'), 'POWER OFF');
INSERT INTO ALERT_EVENT VALUES('PE03', to_date('01/10/2018 00:00:00','dd/mm/yyyy hh24:mi:ss'), 'POWER ON');
查询结果使用 ORACLE 匹配模式 你很接近,试试这个:
SELECT *
FROM ALERT_EVENT ml
MATCH_RECOGNIZE (
PARTITION BY MEASUREMENT_POINT
ORDER BY TIME_ALERT
MEASURES STRT.TIME_ALERT AS start_tstamp,
LAST(END.TIME_ALERT) AS end_tstamp
PATTERN (STRT END*)
DEFINE
STRT AS STRT.STATUS LIKE '%OFF%' ,
END AS END.STATUS LIKE '%ON%'
) MR
ORDER BY MEASUREMENT_POINT ,start_tstamp;
MEASUREMENT_POINT START_TSTAMP END_TSTAMP
================== ===================== ====================
PE01 2018-10-01 00:00:00 2018-10-02 00:00:00
PE01 2018-10-03 00:00:00 2018-10-04 00:00:00
PE02 2018-10-02 22:00:00 2018-10-02 23:00:00
PE02 2018-10-03 00:00:00
PE03 2018-10-02 22:00:00 2018-10-02 23:00:00
PE03 2018-10-04 10:00:00
问题:但是执行到 Oracle 旧版本或 mysql 或 SQL Server,此功能不支持用于模式匹配的 SQL。哪个解决方案可以针对不使用 SQL 进行模式匹配的给定问题进行一般查询?
非常感谢!
【问题讨论】:
【参考方案1】:您可以在 Oracle 12c 之前的版本中执行此操作,方法是创建一个新列进行分组,然后为每个测量点和新分组列查找相关状态的最小/最大 time_alerts。
分组列是通过计算一个运行总和并在每次遇到POWER OFF状态时将其增加1来生成的。
这给了我们一个类似于以下内容的查询:
select measurement_point,
min(case when status = 'POWER OFF' then time_alert end) start_tstamp,
max(case when status = 'POWER ON' then time_alert end) end_tstamp
from (select measurement_point,
time_alert,
status,
sum(case when status = 'POWER OFF' then 1 else 0 end) over (partition by measurement_point order by time_alert) grp
from alert_event)
group by measurement_point, grp
having min(case when status = 'POWER OFF' then time_alert end) is not null
order by measurement_point, min(time_alert);
MEASUREMENT_POINT START_TSTAMP END_TSTAMP
----------------- ------------------- -------------------
PE01 01/10/2018 00:00:00 02/10/2018 00:00:00
PE01 03/10/2018 00:00:00 04/10/2018 00:00:00
PE02 02/10/2018 22:00:00 02/10/2018 23:00:00
PE02 03/10/2018 00:00:00
PE03 02/10/2018 22:00:00 02/10/2018 23:00:00
PE03 04/10/2018 10:00:00
请注意,这会将状态为“POWER ON”的连续行分组为一组;如果这不是您想要的,您将不得不尝试如何生成 grp 列。
【讨论】:
以上是关于Oracle 10g, 11g, MySQL, SQL SERVER (不支持 MATCH RECOGNIZE ) 如何将两个数据行警告关闭电源和电源合并为一个的主要内容,如果未能解决你的问题,请参考以下文章
oracle 10g,11g,是啥意思????是否它的大小??有10或11G那么大?