문제

상당히 다른 위치에 대해 측정 된 온도 값이 포함 된 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

도움이 되었습니까?

해결책

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;

모든 시간이 정확하다고 가정합니다

다른 팁

데이터를 SQL 형식으로 제공 한 Uglysmurf에게 감사드립니다.

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

이 솔루션은 현재 (또는 현재)에 의존하지 않습니다. 최신 기록 된 데이터에서 작동합니다. IDS- 특징 인 Select 문의 유일한 부분은 ''입니다.+ 15 UNITS MINUTE';; 그것은 또한 '로 쓸 수 있습니다.+ INTERVAL(15) MINUTE TO MINUTE'Informix에서 그리고'+ INTERVAL '15' MINUTE'표준 SQL에서 (DBMS가 간격 유형을 지원하는 경우). 테이블에서 DateTime 년에서 분을 사용하는 것은 정보에 따라 다릅니다. 이와 같은 맥락에서, 관심이없는 정보 (예 : 초)를 저장하지 않는 것이 유용합니다.

Pseudo-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

이것을 '좋은'솔루션으로 정의하는지 확실하지 않지만 각 위치에 두 개의 데이터 포인트가 제공되는 것이 작동합니다.

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와 동일하게 나를 이겼습니다!

나는 Informix가 Oracle과 같은 분석 기능을 가지고 있다고 생각하지 않지만, 그렇게한다면 이것이 그것을 사용하기에 훌륭한 장소가 될 것입니다. 다음은 분석 기능 지연과 최대를 사용하는 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 Analytics에 대한 좋은 리소스 : http://www.psoug.org/reference/analytic_functions.html

이와 같은 것을 시도해보십시오. 매우 효율적이지는 않지만 다른 답변 중 일부와 달리 각 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을 고수하려고했습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top