这是一个完整的、经过测试的示例。
CREATE TABLE tbl1 (ID NUMBER, dt DATE, quantity NUMBER);
DELETE FROM tbl1;
insert into tbl1 values (1,to_date('2010-01-04 11:00','YYYY-MM-DD HH24:MI'), 152);
insert into tbl1 values (2,to_date('2010-01-04 11:00','YYYY-MM-DD HH24:MI'), 210);
insert into tbl1 values (1,to_date('2010-01-04 10:45','YYYY-MM-DD HH24:MI'), 132);
insert into tbl1 values (2,to_date('2010-01-04 10:45','YYYY-MM-DD HH24:MI'), 318);
insert into tbl1 values (4,to_date('2010-01-04 10:45','YYYY-MM-DD HH24:MI'), 122);
insert into tbl1 values (1,to_date('2010-01-04 10:30','YYYY-MM-DD HH24:MI'), 1);
insert into tbl1 values (3,to_date('2010-01-04 10:30','YYYY-MM-DD HH24:MI'), 214);
insert into tbl1 values (2,to_date('2010-01-04 10:30','YYYY-MM-DD HH24:MI'), 5515);
insert into tbl1 values (4,to_date('2010-01-04 10:30','YYYY-MM-DD HH24:MI'), 210);
SELECT t.ID
, t.DT
, t.QUANTITY
FROM tbl1 t
,( SELECT ID
, MAX(dt) dt
FROM tbl1
GROUP BY ID ) t2
WHERE t.id = t2.id
AND t.dt = t2.dt
结果:
1 1/4/2010 11:00:00 AM 152
2 1/4/2010 11:00:00 AM 210
3 1/4/2010 10:30:00 AM 214
4 1/4/2010 10:45:00 AM 122
如果您想获取最后 XX 分钟的记录,您可以这样做(在此示例中我使用 500 分钟,将 500 替换为您想要的任何内容):
SELECT t.ID
, t.DT
, t.QUANTITY
FROM tbl1 t
,( SELECT ID
, MAX(dt) dt
FROM tbl1
WHERE dt >= SYSDATE - (500 / 1400)
GROUP BY ID ) t2
WHERE t.id = t2.id
AND t.dt = t2.dt;