从表中捕获最近30分钟的更改

时间:2017-12-05 12:04:40

标签: sql oracle join

我只需要在以下查询中捕获最近30分钟的更改:

  SELECT 
  P.COL1, P.COL2, P.COL3, C.COL1, C.COL2, T.COL1, T.COL2, P.COL4+T.COL4 AS "COL4"
  FROM
  TABLE1 P,
  TABLE2 C,
  TABLE3 T
  WHERE
  PM.COL1(+)  = C.COL1
  AND T.COL2(+) = C.COL2

表1,表2和表3每个都有一个修改日期列,但我认为在查询中添加以下内容在逻辑上是不对的,它可能会降低查询执行性能

 SELECT 
 P.COL1, P.COL2, P.COL3, C.COL1, C.COL2, T.COL1, T.COL2, P.COL4+T.COL4 AS "COL4" 
 FROM
 TABLE1 P,
 TABLE2 C,
 TABLE3 T
 WHERE
 PM.COL1(+)  = C.COL1
 AND T.COL2(+) = C.COL2
 AND P.MODIFICATION_DATE > SYSDATE - 30
 AND C.MODIFICATION_DATE > SYSDATE - 30 
 AND T.MODIFICATION_DATE > SYSDATE - 30;

同样不是所有3个表同时更新,TABLE2作为示例,last modification_date是昨天上午11:00,而TABLE3 modification_date是今天下午2:00。

如何捕捉表格之间最近30分钟的变化?

2 个答案:

答案 0 :(得分:1)

您的查询有多种不同方式。首先,sysdate - 30从值中减去30 而不是30 分钟

根据您的描述,以下内容可能是您想要的:

SELECT . . .
FROM (SELECT P.*
      FROM TABLE1 P
      WHERE P.MODIFICATION_DATE > SYSDATE - 30 / (24 * 60)
     ) P FULL JOIN
     (SELECT C.*
      FROM TABLE2 C
      WHERE C.MODIFICATION_DATE > SYSDATE - 30 / (24 * 60)
     ) C
     ON P.COL1 = C.COL1 FULL JOIN
     (SELECT T.*
      FROM TABLE3 T
      WHERE T.MODIFICATION_DATE > SYSDATE - 30 / (24 * 60)
     )
     ON T.COL2 = C.COL2;

答案 1 :(得分:0)

  

如何捕捉表格之间最近30分钟的变化?

您可以使用以下网址从一个表中找到每个COL1COL2的最近30分钟活动:

SELECT *
FROM   (
  SELECT C.*,
         MAX( MODIFICATION_DATE ) OVER ( PARTITION BY COL1, COL2 ) AS last_mod_date
  FROM   TABLE2 C
)
WHERE  MODIFICATION_DATE >= LAST_MOD_DATE - INTERVAL '30' MINUTE

如果您只想要整个表的最后30分钟(而不是COL1COL2的每个组合),请删除PARTITION BY COL1, COL2子句,将其保留为{{ 1}}。

所以,要获得每张桌子的最后30分钟,你可以这样做:

OVER ()

如果没有一些示例数据并看到您的预期结果,则很难知道您是否需要/ WITH C AS ( SELECT * FROM ( SELECT C.*, MAX( MODIFICATION_DATE ) OVER ( PARTITION BY COL1, COL2 ) AS last_mod_date FROM TABLE2 C ) WHERE MODIFICATION_DATE >= LAST_MOD_DATE - INTERVAL '30' MINUTE ), P AS ( SELECT * FROM ( SELECT P.*, MAX( MODIFICATION_DATE ) OVER ( PARTITION BY COL1 ) AS last_mod_date FROM TABLE1 P ) WHERE MODIFICATION_DATE >= LAST_MOD_DATE - INTERVAL '30' MINUTE ), T AS ( SELECT * FROM ( SELECT T.*, MAX( MODIFICATION_DATE ) OVER ( PARTITION BY COL2 ) AS last_mod_date FROM TABLE2 T ) WHERE MODIFICATION_DATE >= LAST_MOD_DATE - INTERVAL '30' MINUTE ) SELECT P.COL1, P.COL2, P.COL3, C.COL1, C.COL2, T.COL1, T.COL2, P.COL4+T.COL4 AS "COL4" FROM C LEFT OUTER JOIN P ON ( P.COL1 = C.COL1 ) LEFT OUTER JOIN T ON ( T.COL2 = C.COL2 ); 条款。