我不认为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