在oracle10g、11g、mysql、sqlserver中如何将掉电和开机两个数据行合并为一个数据行(不支持匹配识别)

x8diyxa7  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(315)

https://drive.google.com/file/d/1yakmtd5m2meindbeg2ckutb3vw7ypfto/view?usp=sharing
大家好,
我已经说过,给定的问题与链接和被解决的sql模式匹配解决方案是支持或oracle 12c由wernfried domscheit的信息:

  1. CREATE TABLE ALERT_EVENT (MEASUREMENT_POINT VARCHAR2(10), TIME_ALERT DATE, STATUS VARCHAR2(10));
  2. INSERT INTO ALERT_EVENT VALUES('PE01', to_date('04/10/2018 00:00:00','dd/mm/yyyy hh24:mi:ss'), 'POWER ON');
  3. INSERT INTO ALERT_EVENT VALUES('PE01', to_date('03/10/2018 00:00:00','dd/mm/yyyy hh24:mi:ss'), 'POWER OFF');
  4. INSERT INTO ALERT_EVENT VALUES('PE01', to_date('02/10/2018 00:00:00','dd/mm/yyyy hh24:mi:ss'), 'POWER ON');
  5. INSERT INTO ALERT_EVENT VALUES('PE01', to_date('01/10/2018 00:00:00','dd/mm/yyyy hh24:mi:ss'), 'POWER OFF');
  6. INSERT INTO ALERT_EVENT VALUES('PE02', to_date('03/10/2018 00:00:00','dd/mm/yyyy hh24:mi:ss'), 'POWER OFF');
  7. INSERT INTO ALERT_EVENT VALUES('PE02', to_date('02/10/2018 23:00:00','dd/mm/yyyy hh24:mi:ss'), 'POWER ON');
  8. INSERT INTO ALERT_EVENT VALUES('PE02', to_date('02/10/2018 22:00:00','dd/mm/yyyy hh24:mi:ss'), 'POWER OFF');
  9. INSERT INTO ALERT_EVENT VALUES('PE03', to_date('04/10/2018 10:00:00','dd/mm/yyyy hh24:mi:ss'), 'POWER OFF');
  10. INSERT INTO ALERT_EVENT VALUES('PE03', to_date('02/10/2018 23:00:00','dd/mm/yyyy hh24:mi:ss'), 'POWER ON');
  11. INSERT INTO ALERT_EVENT VALUES('PE03', to_date('02/10/2018 22:00:00','dd/mm/yyyy hh24:mi:ss'), 'POWER OFF');
  12. INSERT INTO ALERT_EVENT VALUES('PE03', to_date('01/10/2018 00:00:00','dd/mm/yyyy hh24:mi:ss'), 'POWER ON');

查询结果使用oracle匹配模式您非常接近,请尝试以下方法:

  1. SELECT *
  2. FROM ALERT_EVENT ml
  3. MATCH_RECOGNIZE (
  4. PARTITION BY MEASUREMENT_POINT
  5. ORDER BY TIME_ALERT
  6. MEASURES STRT.TIME_ALERT AS start_tstamp,
  7. LAST(END.TIME_ALERT) AS end_tstamp
  8. PATTERN (STRT END*)
  9. DEFINE
  10. STRT AS STRT.STATUS LIKE '%OFF%' ,
  11. END AS END.STATUS LIKE '%ON%'
  12. ) MR
  13. ORDER BY MEASUREMENT_POINT ,start_tstamp;
  14. MEASUREMENT_POINT START_TSTAMP END_TSTAMP
  15. ================== ===================== ====================
  16. PE01 2018-10-01 00:00:00 2018-10-02 00:00:00
  17. PE01 2018-10-03 00:00:00 2018-10-04 00:00:00
  18. PE02 2018-10-02 22:00:00 2018-10-02 23:00:00
  19. PE02 2018-10-03 00:00:00
  20. PE03 2018-10-02 22:00:00 2018-10-02 23:00:00
  21. PE03 2018-10-04 10:00:00

问题:但执行到oracle旧版本或mysql或sql server时,此函数不支持用于模式匹配的sql。对于不使用sql进行模式匹配的给定问题,要查询常规的解决方案是什么?
非常感谢!

deikduxw

deikduxw1#

在oracle 12c之前的版本中,您可以通过创建一个新列来进行分组,然后根据测量点和新的分组列查找相关状态的最小/最大时间警告。
分组列是通过计算运行和并在每次遇到电源关闭状态时将其增加1来生成的。
这给了我们一个大致的问题:

  1. select measurement_point,
  2. min(case when status = 'POWER OFF' then time_alert end) start_tstamp,
  3. max(case when status = 'POWER ON' then time_alert end) end_tstamp
  4. from (select measurement_point,
  5. time_alert,
  6. status,
  7. sum(case when status = 'POWER OFF' then 1 else 0 end) over (partition by measurement_point order by time_alert) grp
  8. from alert_event)
  9. group by measurement_point, grp
  10. having min(case when status = 'POWER OFF' then time_alert end) is not null
  11. order by measurement_point, min(time_alert);
  12. MEASUREMENT_POINT START_TSTAMP END_TSTAMP
  13. ----------------- ------------------- -------------------
  14. PE01 01/10/2018 00:00:00 02/10/2018 00:00:00
  15. PE01 03/10/2018 00:00:00 04/10/2018 00:00:00
  16. PE02 02/10/2018 22:00:00 02/10/2018 23:00:00
  17. PE02 03/10/2018 00:00:00
  18. PE03 02/10/2018 22:00:00 02/10/2018 23:00:00
  19. PE03 04/10/2018 10:00:00

请注意,这将把状态为“开机”的连续行分组为一组;如果您不希望生成grp列,那么您必须仔细研究它是如何生成的。

展开查看全部

相关问题