将Date和Second添加到Timestamp,其中Date是相同的

时间:2013-11-08 08:16:26

标签: oracle oracle11g oracle10g

假设我有Timestamp1

01.10.2013 10:00:00

Timestamp1是来自tbl1的最大值(时间戳)。 现在我需要来自tbl2的max(timestamp2)的Timestamp1 +秒和分钟,其中DATE是相同的。

1 个答案:

答案 0 :(得分:1)

试试这个:

CREATE TABLE tbl1 (
  timestamp1 TIMESTAMP
);

CREATE TABLE tbl2 (
  timestamp2 TIMESTAMP
);

INSERT INTO tbl1 VALUES (TO_TIMESTAMP('01.10.2013 09:00:00', 'DD.MM.YYYY HH24:MI:SS'));
INSERT INTO tbl1 VALUES (TO_TIMESTAMP('01.10.2013 10:00:00', 'DD.MM.YYYY HH24:MI:SS'));
INSERT INTO tbl1 VALUES (TO_TIMESTAMP('02.10.2013 10:32:54', 'DD.MM.YYYY HH24:MI:SS'));
INSERT INTO tbl1 VALUES (TO_TIMESTAMP('03.10.2013 10:00:00', 'DD.MM.YYYY HH24:MI:SS'));

INSERT INTO tbl2 VALUES (TO_TIMESTAMP('01.10.2013 12:24:33', 'DD.MM.YYYY HH24:MI:SS'));
INSERT INTO tbl2 VALUES (TO_TIMESTAMP('02.10.2013 12:46:11', 'DD.MM.YYYY HH24:MI:SS'));

COMMIT;

SELECT t1.max_time +
          NUMTODSINTERVAL(NVL(EXTRACT(MINUTE FROM t2.max_time), 0), 'MINUTE') +
          NUMTODSINTERVAL(NVL(EXTRACT(SECOND FROM t2.max_time), 0), 'SECOND') AS val
  FROM (SELECT TRUNC(timestamp1) date_part, MAX(timestamp1) max_time
          FROM tbl1
        GROUP BY TRUNC(timestamp1)) t1
    LEFT JOIN (SELECT TRUNC(timestamp2) date_part, MAX(timestamp2) max_time
            FROM tbl2
          GROUP BY TRUNC(timestamp2)) t2
    ON (t1.date_part = t2.date_part)
;

输出:

VAL                        
----------------------------
13/10/01 10:24:33,000000000  
13/10/02 11:19:05,000000000 
13/10/03 10:00:00,000000000