我不認爲Informix具有像Oracle這樣的分析功能,但如果這樣做的話,這將是一個很好的使用它們的地方。以下是使用解析函數lag和max的Oracle示例。
安裝腳本:
drop table temps;
create table temps (
locId varchar2(3),
dtg date,
temp number(3)
);
insert into temps values ('aaa', to_date('2009-02-25 10:00','yyyy-mm-dd hh:mi'), 15);
insert into temps values ('bbb', to_date('2009-02-25 10:00','yyyy-mm-dd hh:mi'), 20);
insert into temps values ('ccc', to_date('2009-02-25 10:00','yyyy-mm-dd hh:mi'), 24);
insert into temps values ('aaa', to_date('2009-02-25 09:45','yyyy-mm-dd hh:mi'), 13);
insert into temps values ('ccc', to_date('2009-02-25 09:45','yyyy-mm-dd hh:mi'), 16);
insert into temps values ('bbb', to_date('2009-02-25 09:45','yyyy-mm-dd hh:mi'), 18);
insert into temps values ('ddd', to_date('2009-02-25 09:45','yyyy-mm-dd hh:mi'), 12);
insert into temps values ('aaa', to_date('2009-02-25 09:30','yyyy-mm-dd hh:mi'), 11);
insert into temps values ('ccc', to_date('2009-02-25 09:30','yyyy-mm-dd hh:mi'), 14);
insert into temps values ('bbb', to_date('2009-02-25 09:30','yyyy-mm-dd hh:mi'), 15);
insert into temps values ('ddd', to_date('2009-02-25 09:30','yyyy-mm-dd hh:mi'), 10);
commit;
Oracle特有的查詢使用分析功能:
select locId, change
from (
select t.locId,
t.dtg,
t.temp,
-- difference between this records temperature and the record before it
t.temp - lag(t.temp) over (partition by t.locId order by t.dtg) change,
-- max date for this location
max(t.dtg) over (partition by t.locId) maxDtg,
max(t.dtg) over (partition by 1) overallMaxDtg
from temps t
order by t.locId, t.dtg
) where maxDtg = dtg -- only most recent measurement
and overallMaxDtg = maxDtg -- only stations with an 'updated measurement'
;
結果:
LOCID CHANGE
aaa 2
bbb 2
ccc 8
甲骨文分析良好的資源:http://www.psoug.org/reference/analytic_functions.html
爲什麼不包括ddd?它有一個入口在9:30,溫度爲10,另一個入口在9點45分,溫度爲12。 – jimmyorr 2009-02-25 23:15:59
只有包含10:00和9:45數據的站點將被包括 – BCS 2009-02-26 01:19:58