SQL查找两行之间的差异

时间:2009-02-25 22:34:38

标签: sql informix

我有一个Informix数据库,其中包含很多不同位置的测量温度值。对于所有位置,每15分钟进行一次测量,然后将时间戳加载到同一个表中。表看起来像这样:

locId    dtg               temp
aaa      2009-02-25 10:00  15
bbb      2009-02-25 10:00  20
ccc      2009-02-25 10:00  24
aaa      2009-02-25 09:45  13
ccc      2009-02-25 09:45  16
bbb      2009-02-25 09:45  18
ddd      2009-02-25 09:45  12
aaa      2009-02-25 09:30  11
ccc      2009-02-25 09:30  14
bbb      2009-02-25 09:30  15
ddd      2009-02-25 09:30  10

现在我想要一个查询,告诉我所有电台的最后两次测量之间的温度变化。而且,只有具有更新测量值的那些。例如,在上表中,不包括位置ddd。 结果就是:

locId    change
aaa      2
bbb      2
ccc      8

我尝试了很多,但我找不到任何好的解决方案。实际上,从网页上询问大约700个位置,所以我认为查询需要相当高效。

真的很感激一些帮助! //的Jesper

6 个答案:

答案 0 :(得分:4)

set now = select max(dtg) from table;
set then = select max(dtg) from table where dtg < now;

select locID, old.temp-new.temp from 
      table as old join
      table as new 
      on old.locId = new.locID
where
      old.dtg = then and
      new.dtg = now;

假设所有时间都是准确的

答案 1 :(得分:4)

感谢uglysmurf提供SQL格式的数据。

使用IDS(IBM Informix Dynamic Server)版本11.50,以下查询有效。

CREATE TEMP TABLE temps
(
    locId   CHAR(3),
    dtg     DATETIME YEAR TO MINUTE,
    temp    SMALLINT
);
INSERT INTO temps VALUES ('aaa', '2009-02-25 10:00', 15);
INSERT INTO temps VALUES ('bbb', '2009-02-25 10:00', 20);
INSERT INTO temps VALUES ('ccc', '2009-02-25 10:00', 24);
INSERT INTO temps VALUES ('aaa', '2009-02-25 09:45', 13);
INSERT INTO temps VALUES ('ccc', '2009-02-25 09:45', 16);
INSERT INTO temps VALUES ('bbb', '2009-02-25 09:45', 18);
INSERT INTO temps VALUES ('ddd', '2009-02-25 09:45', 12);
INSERT INTO temps VALUES ('aaa', '2009-02-25 09:30', 11);
INSERT INTO temps VALUES ('ccc', '2009-02-25 09:30', 14);
INSERT INTO temps VALUES ('bbb', '2009-02-25 09:30', 15);
INSERT INTO temps VALUES ('ddd', '2009-02-25 09:30', 10);

SELECT latest.locID, latest.temp, prior.temp,
       latest.temp - prior.temp as delta_temp,
       latest.dtg, prior.dtg
    FROM temps latest, temps prior
    WHERE latest.locId = prior.locId
      AND latest.dtg = prior.dtg + 15 UNITS MINUTE
      AND latest.dtg = (SELECT MAX(dtg) FROM temps);

结果(列数多于请求的列数,但您可以轻松修剪选择列表):

aaa 15 13 2 2009-02-25 10:00 2009-02-25 09:45
ccc 24 16 8 2009-02-25 10:00 2009-02-25 09:45
bbb 20 18 2 2009-02-25 10:00 2009-02-25 09:45

请注意,此解决方案不依赖于CURRENT(或NOW);它适用于最新记录的数据。 SELECT语句中唯一特定于IDS的部分是“+ 15 UNITS MINUTE”;也可以在Informix中写为“+ INTERVAL(15) MINUTE TO MINUTE”,在标准SQL中写为“+ INTERVAL '15' MINUTE”(如果DBMS支持INTERVAL类型)。在表中使用DATETIME YEAR TO MINUTE是特定于Informix的;在这样的上下文中,不存储您不感兴趣的信息(例如秒)是有用的。

答案 2 :(得分:2)

在伪SQL中,您可以执行查询:

@now = Time Now

Select Oldest.LocId, Oldest.timestamp, Oldest.temp - Newest.temp as Change
(Select LocId, temp from Foo where timestamp < @now - 15 mins AND timestamp >= @now - 30 mins) Oldest
   left join
(Select LocId, temp from Foo where timestamp >= TimeNow - 15 mins) Newest
   on Oldest.LocId = Newest.LocId

不确定您是否将此定义为“良好”解决方案,但它应该有效,前提是每个位置都有两个数据点。

答案 3 :(得分:1)

declare @dt_latest datetime, @dt_prev datetime  

select @dt_latest = max(dtg) from Measures
select @dt_prev = max(dtg) from Measures where dtg < @dt_latest  

select Latest.Locid, Latest.temp - Prev.temp
from Measures as "Latest"
inner join Measures as "Prev" on Latest.Locid = Prev.Locid
where Latest.dtg = @dt_latest
and Prev.dtg = @dt_prev

编辑:基本上和BCS一样,打败我吧!

答案 4 :(得分:1)

我不相信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

Oracle分析的良好资源:http://www.psoug.org/reference/analytic_functions.html

答案 5 :(得分:0)

尝试这样的东西。它可能不是超级高效的,但与其他一些答案不同,它将为每个LocID返回dif

SELECT DISTINCT LocID,
            (SELECT max(t3.temp)-min(t3.temp) from 
                   (SELECT TOP 2 T2.temp 
                    From Table2 T2 
                    Where (t2.Locid=t1.locid) 
                    order by DTG DESC) as t3
             ) as Diff
     FROM Table1 T1

警告:我使用tSQL编写了这个,但是尽可能地坚持使用标准ANSI SQL来实现对Informix的可移植性。

相关问题