2014-01-27 2 views
0

타임 스탬프, 가격 및 시세 열이있는 테이블이 있다고 가정 해 보겠습니다. 모든 시간 소인 값은 분 경계에 있지만 모든 분이 데이터에 존재하는 것은 아닙니다. 이 분에 대한 행이 없습니다.MySQL 데이터 세트에서 누락 된 시간 채우기

누락 된 분의 행을 이 채워진 다음 행의 가격으로 채우는 순수 SQL 방식이 있습니까?

개선 사항은 아직까지는 오전 9시 30 분부터 동부 시간 오후 4 시까 지 분 단위로만이 방법으로 다시 채워질 것입니다. 행을 가정

+1

행이 누락 되었습니까? 아니면 행에 데이터가 누락되어 모든 분의 행이 있습니까? –

+0

예 행이 누락되었습니다. 더 명확하지 않게해서 죄송합니다. 질문을 업데이트하겠습니다. –

답변

1

데이터에 있지만 값, 다음은 행의 이전 값을 가져옵니다

update t join 
     (select t.timestamp, 
       (select value 
       from t t2 
       where t2.timestamp < t.timestamp and t2.value is not null 
       order by t2.timestamp desc 
       limit 1 
       ) as value 
     from t 
     ) toupdate 
     on t.timestamp = toupdate.timestamp 
    set t.value = toupdate.value; 

: 당신은 쉽게 업데이 트에이를 넣을 수 있습니다

select t.timestamp, 
     coalesce(value, 
       (select value 
       from t t2 
       where t2.timestamp < t.timestamp and t2.value is not null 
       order by t2.timestamp desc 
       limit 1 
       ) 
       ) as value 
from t; 

편집 :

행이 누락 된 경우이를 복제해야합니다. insert

select t.timestamp + interval 1 minute, value 
from t left outer 
    t tnext 
    on tnext.timestamp = t.timestamp + interval 1 minute; 
where tnext.timestamp is null 

당신은이를 삽입 할 수 있습니다 : 한 행에없는 가정하면, 다음이 값을 얻을 일분보다 큰 격차를 들어

insert into t(timestamp, value) 
    select t.timestamp + interval 1 minute, value 
    from t left outer 
     t tnext 
     on tnext.timestamp = t.timestamp + interval 1 minute; 
    where tnext.timestamp is null; 

, 나는 단순히 반복 제안 새로운 행이 발견되지 않을 때까지 insert.

관련 문제