2016-05-30 9 views
0

一般的なデータウェアハウスの問題があります。SQLの欠落した日付

私がやっていることは、日付間にギャップがないSQLクエリを作成することです。

ファクトテーブルは、測定されてテーブルに記録されたレコードのみを登録します。このケースは、海上の船舶に登録された石油生産に関するものです。いくつかの日には船の一部にはプロダクションはありません。

私の単純なデータウェアハウスはDimVessel,DimDateおよびFactProductionです。そして、あなたの下に、私が望む最終的な結果セットが表示されます。

容器に生産物がない場合でも、ファクトテーブルにvesselkeyProductionを記入するSQLクエリが必要です。いくつかの船には生産があり、他の日にはすべての船があることを忘れないでください(最善のケース)。

誰かが私をここで助けてくれることを願っています。

DimVessel

VesselKey| VesselName 
1  | Vessel 1 
2  | Vessel 2 
3  | Vessel 3 
4  | Vessel 4 
5  | Vessel 5 

DimDate

DateKey  |Date 
20160517 |2016-05-17 00:00:00.000 
20160518 |2016-05-18 00:00:00.000 
20160519 |2016-05-19 00:00:00.000 
20160520 |2016-05-20 00:00:00.000 

FactProduction

DateKey  |VesselKey |Production 
20160517 |4   |12505 
20160517 |5   |1276 
20160517 |3   |88 
20160517 |2   |3919 
20160518 |4   |8785 
20160518 |5   |736 
20160518 |1   |3754 
20160518 |2   |5654 
20160519 |2   |1654 
20160520 |1   |2016 
20160520 |3   |6059 
20160520 |4   |10980 
20160520 |5   |663 

これはどのようなIワシントン州最終結果セット:

DateKey  |VesselKey |Production 
20160517 |4   |12505 
20160517 |5   |1276 
20160517 |3   |88 
20160517 |2   |3919 
20160517 |1   |0 
20160518 |4   |8785 
20160518 |5   |736 
20160518 |3   |0 
20160518 |1   |3754 
20160518 |2   |5654 
20160519 |2   |1654 
20160519 |1   |0 
20160519 |3   |0 
20160519 |4   |0 
20160519 |5   |0 
20160520 |1   |2016 
20160520 |3   |6059 
20160520 |2   |4059 
20160520 |4   |10980 
20160520 |5   |663 
+0

誰かがあなたを確認するのに役立ちます。人間が読めるように質問を書式化した場合、これまで行ってきたことを示して、あなたの問題が何であるか教えてください。 –

+0

左の結合に精通していますか? –

+0

私はあなたの質問の文法にいくつかの訂正をしました。彼らが何らかの形で意味を変えたら、私の編集を元に戻してください。 –

答えて

0

なぜ存在しないデータを保存したいのですか?私は、ソースシステムに物理的に存在しないファクトデータを格納するファンではありません。

あなたはあなたが単にするのではなく、ソース・データに参加追加する必要があり、データを保存したいことを主張した場合(テストしていません)以下のクエリ

SELECT DD.DateKey 
,DV.VesselKey 
,Production = ISNULL(FP.Production) 
FROM DimDate DD 
    INNER JOIN DimVessel DV ON 1 = 1 
    LEFT JOIN FactProduction FP ON (FP.DateKey = DD.DateKey AND FP.VesselKey = DV.VesselKey) 
WHERE DD.DateKey BETWEEN @StartDate AND @EndDate 

で欲しい結果を得ることができます

select allCombinations.DateKey, 
     allCombinations.VesselKey, 
     isnull(p.Production, 0) as Production 
from (
     select d.DateKey, 
       v.VesselKey 
     from @Dates as d 
     cross join @Vessels as v 
     ) as allCombinations 
left join @Production as p 
on  allCombinations.DateKey = p.DateKey 
     and allCombinations.VesselKey = p.VesselKey 

このソリューションを検証するために、完全なコードは以下です:FactProductionテーブルは、その後、あなたの実際ここで

+0

存在しないデータについては、dimDateテーブルを参照していますか? –

+0

確かに、私は事実のデータを言った。 DimDateは次元です。我々が意味論に入りたいのであれば、DimDateはカレンダー(Pseudo)から供給されます。ファクトテーブルにないIMOレコードは、ギャップを埋めるためにレコードを作成するよりも有益です。ソースデータの問題を示している可能性があります。データがまだ届いていない/まだ届いていません。 – DamutuMike

+0

こんにちは、ありがとうございました。プロダクションが停止している場合は、ここのようにデータが登録されていないことがあります。タイムライン0では、たとえそれが決して測定されないとしても、生産はデータでもあります。グラフやグラフで表示すると、データに穴ができます。理解できますが、誤解されることもあります。 – user5767413

0

に結果を挿入することは実用的なソリューションでありますINGの:

declare @Vessels table 
    (
    VesselKey int primary key 
        not null, 
    VesselName as (N'Vessel ' + cast(VesselKey as nvarchar)) 
    ) 

insert into @Vessels 
     (VesselKey) 
values (1), 
     (2), 
     (3), 
     (4), 
     (5) 

select * 
from @Vessels as v 

declare @Dates table 
    (
    DateKey int primary key 
       not null, 
    Date as (try_convert(datetime2, cast(DateKey as nvarchar) 
       + ' 00:00:00.000')) 
    ) 

insert into @Dates 
     (DateKey) 
values (20160517), 
     (20160518), 
     (20160519), 
     (20160520) 

select * 
from @Dates as d 

declare @Production table 
    (
    DateKey int, 
    VesselKey int, 
    Production int 
    ) 

insert into @Production 
     (DateKey, VesselKey, Production) 
values (20160517, 4, 12505), 
     (20160517, 5, 1276), 
     (20160517, 3, 88), 
     (20160517, 2, 3919), 
     (20160518, 4, 8785), 
     (20160518, 5, 736), 
     (20160518, 1, 3754), 
     (20160518, 2, 5654), 
     (20160519, 2, 1654), 
     (20160520, 1, 2016), 
     (20160520, 3, 6059), 
     (20160520, 4, 10980), 
     (20160520, 5, 663) 

select * 
from @Production as p 



select allCombinations.DateKey, 
     allCombinations.VesselKey, 
     isnull(p.Production, 0) as Production 
from (
     select d.DateKey, 
       v.VesselKey 
     from @Dates as d 
     cross join @Vessels as v 
     ) as allCombinations 
left join @Production as p 
on  allCombinations.DateKey = p.DateKey 
     and allCombinations.VesselKey = p.VesselKey 

結果は、あなたが探していたものではない場合、私に教えてください。

+0

はい、ありがとうございます。そして、私は、より詳細なデータを時間単位で必要とする2番目のクエリで推測します。このクエリーは、DimTimeのTimeKey(0-1439)を追加するだけで使用できます。この2番目のクエリは、昨日の午前10時16分、今日の午前10時15分、昨日の午前10時21分から今日の午前10時20分までの24時間を示すスライディングウィンドウです。私は新しいケースでもこのクエリを使用しようとします。また、非常に速い応答に感謝します。ガイアについて – user5767413