2016-05-30 99 views
0

我有我猜測的是一個常見的數據倉庫問題。SQL填寫缺少日期

我一直在試圖做的是做一個SQL日期之間沒有差距的查詢。

事實表只記錄被測量並記錄到表中的記錄。這個案例涉及海上船隻的註冊石油生產。有些日子有些船上沒有生產。

所以這裏是我的簡單數據倉庫:DimVessel,DimDateFactProduction。在下面你看到我想要的最終結果集。

我希望SQL Query能夠用vesselkeyProduction填充事實表,即使船上沒有生產。請記住,有些日子可能會在少數船舶上生產,其他日子可能會生產所有船舶(最好的情況)。

我希望有人能幫助我。

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 

這是我はnt,最終結果集:

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 

如果你堅持想保存數據,那麼你只需要添加一個連接到您的源數據,而不是結果FactProduction表,然後將結果插入到你的事實

+0

關於不存在的數據,你是指dimDate表嗎? –

+0

不太確定我說的是事實數據。 DimDate是一個維度。如果我們想進入語義學,那麼DimDate就來自日曆(Pseudo)。國際海事組織從事實數據表中缺失的記錄比創建填補空白的記錄更具信息量。可能表示源數據問題或只是突出顯示數據尚未完成/尚未完成 – DamutuMike

+0

嗨,感謝所有評論。有時候像這裏生產停止時一樣,沒有數據被註冊。在時間線0生產也是數據,即使它沒有以任何方式測量。當以圖表或圖表形式呈現時,您的數據中會出現漏洞。可能是可以理解的,但也可能會被誤解。 – user5767413

0

這裏是一個有效的解決方案:

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 

爲了驗證該解決方案的完整的代碼是後續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

是的,謝謝。我猜想在第二個查詢中,我希望按小時更詳細的數據,我可以使用此查詢僅添加DimTime中的TimeKey(0-1439),並在白天每分鐘註冊一個生產樣本。第二個查詢將顯示過去24小時的滑動窗口,如昨天上午10點16分至今天上午10點15分,以及5分鐘後上午10點21分至今天上午10點20分。我也會嘗試在我的新案例中使用這個查詢。並再次感謝非常快速的響應。問候蓋爾 – user5767413