插入 MATCH_RECOGNIZE select Oracle 的第一行和最后一行

Insert into first and last row of a MATCH_RECOGNIZE select Oracle

我不太擅长 sql,我需要你的帮助。

我在 Oracle 数据库中得到了一个 table 示例数据,如下所示:

| DATE_TIME                 | TYPE | LAT   | LON   | MSG   |
| :------------------------ | :--- | :---- | :---- | :---- |
| 12.11.20 10:04.00.0000000 | A    | 10.03 | 11.15 |       |
| 12.11.20 10:05.00.0000000 | A    | 10.04 | 11.14 |       |
| 12.11.20 10:06.00.0000000 | A    | 10.05 | 11.13 |       |
| 12.11.20 11:18.00.0000000 | B    | 20.45 | 40.58 |       |
| 12.11.20 11:19.00.0000000 | B    | 20.46 | 40.59 |       |
| 12.11.20 11:20.00.0000000 | B    | 20.47 | 40.60 |       |

我有一个 select,它给了我 START_TIME、END_TIME、类型、START_LAT、START_LON、END_LAT, END_LON, START_MSG, END_MSG:

SELECT 
  TO_CHAR(START_TIME, 'DD.MM.YYYY HH24:MI') AS START_TIME,
  TO_CHAR(END_TIME, 'DD.MM.YYYY HH24:MI') AS END_TIME,
  TYPE,
  START_LAT,
  START_LON,
  END_LAT,
  END_LON,
  START_MSG,
  END_MSG
FROM TABLE_1
  MATCH_RECOGNIZE (
    PARTITION BY TYPE
    ORDER BY DATE_TIME
    MEASURES
      FIRST(DATE_TIME) AS START_TIME,
      LAST(DATE_TIME) AS END_TIME,
      FIRST(LAT) AS START_LAT,
      FIRST(LON) AS START_LON,
      LAST(LAT) AS END_LAT,
      LAST(LON) AS END_LON,
      FIRST(MSG) AS START_MSG,
      LAST(MSG) AS END_MSG
    ONE ROW PER MATCH
    PATTERN ( adjacent_minutes * last_minute )
    DEFINE adjacent_minutes AS NEXT(DATE_TIME) <= LAST(DATE_TIME) + INTERVAL '5' MINUTE
  );

此 select 的输出:

| START_TIME     | END_TIME       | TYPE | START_LAT | START_LON | END_LAT | END_LON | START_MSG | END_MSG |
| :------------- | :------------- | :--- | :-------- | :-------- | :------ | :------ | :-------- | :------ |
| 12.11.20 10:04 | 12.11.20 10:06 | A    | 10.03     | 11.15     | 10.05   | 11.13   |           |         |
| 12.11.20 11:18 | 12.11.20 11:20 | B    | 20.45     | 40.58     | 20.47   | 40.60   |           |         |

现在我需要将值为“country xy”的数据插入到列 START_MSG 中,例如 START_LAT在10和11之间,START_LON在11和12之间。END_MSG也一样。

我该怎么做?

抱歉我的英语不好 ;)。

在此先感谢您!

您可以使用CASE WHEN表达式如下:

SELECT ...
  ....
  END_LAT,
  END_LON,
  CASE WHEN START_LAT BETWEEN 10 AND 11 AND START_LON BETWEEN 11 AND 12 
       THEN 'country xy' 
       ELSE START_MSG 
  END AS START_MSG,
  CASE WHEN START_LAT BETWEEN 10 AND 11 AND START_LON BETWEEN 11 AND 12 
       THEN 'country xy' 
       ELSE END_MSG 
  END AS END_MSG
FROM TABLE_1
  MATCH_RECOGNIZE (
    ...

或者在子查询中使用整个查询,在外部查询中对这两列使用此 CASE..WHEN