順序付けられたテーブルの最後の既知の値にヌルを更新するためのソリューションを設定します

StackOverflow https://stackoverflow.com/questions/4922856

  •  30-10-2019
  •  | 
  •  

質問

データベースのDorkとしての私の純度と名誉を維持するために、各ヌル値が以前の(DateTime)非ヌル値に置き換えるように、フロート列にいくつかのヌルを持つデータタイムとフロートのセットを更新したいと思います。

私の環境はMSSQL 2K8R2です。

次のスニペットが私の勝利条件を十分に説明することを願っています。

乾杯とありがとう。

create table #datesAndValues(
    [Date] datetime,
    Val float);
create table #resultsShouldLookLikeThis(
    [Date] datetime,
    Val float);

insert into #datesAndValues 
values  
    ('1/8/11',1.1),
    ('1/7/11',null),
    ('1/6/11',1.2),
    ('1/5/11',null),
    ('1/4/11',null),
    ('1/3/11',1.3),
    ('1/2/11',null),
    ('1/1/11',null);

/*  
    set based code (no loops!!!) goes here to set nulls
    in the above table to the last known good (not null) value,
    thereby making it look like the following table.
*/

insert into #resultsShouldLookLikeThis
values  
    ('1/8/11',1.1),
    ('1/7/11',1.2),
    ('1/6/11',1.2),
    ('1/5/11',1.3),
    ('1/4/11',1.3),
    ('1/3/11',1.3),
    ('1/2/11',null),
    ('1/1/11',null);

--this will tell us if it worked
select 'In old but not new' as [WhichTable], o.*
from    (select * from #datesAndValues
        except select * from #resultsShouldLookLikeThis) o
union all
select 'In new but not old' as [WhichTable], n.*
from    (select * from #resultsShouldLookLikeThis
        except select * from #datesAndValues) n;

drop table #datesAndValues;
drop table #resultsShouldLookLikeThis;

正しい解決策はありません

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top