2014-02-11 21 views
1

我很新的SQL Server的所以這可能是一個非常簡單的問題SQL Server:在日期過濾來自另一個表

我有一個看起來像下面這樣的日誌表視圖:

enter image description here

並記錄了一個運行,看起來像下面的啓動和停止表:

enter image description here

竟被我d喜歡在第二個表中的開始和停止時間之間過濾第一個表中的數據。有沒有辦法做到這一點?

+1

你是否試圖自己解決這個問題?如果是這樣,請在問題中包含SQL。 –

+2

討論SQL查詢時,表名通常很有用。 –

回答

1

您可以使用常規join匹配日期範圍:

select * 
from Entries e 
join Runs r 
on  r.StartTime <= e.EntryTime 
     and (
      e.EntryTime <= r.StopTime 
      or r.StopTime is null -- For a run that has not yet stopped 
     ) 

但你的例子數據表明有一個外鍵,這將消除對日期範圍匹配的需要:

select * 
from Entries e 
join Runs r 
on  e.ProjectRunId = r.ProjectRunId