Lets say I got Timestamp1
01.10.2013 10:00:00
Timestamp1 is the max(timestamp) from tbl1. Now I need Timestamp1 + seconds and minutes from max(timestamp2) from tbl2 where the DATE is the same.
尝试这个:
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)
;
输出:
价值 ---------------------------- 13/10/01 10:24:33,000000000 2002 年 13 月 10 日 11:19:05,000000000 2003 年 13 月 10 日 10:00:00,000000000