連接SQL表我有如下表,表1十字按日期
date value
-------------------------
2015-01-01 0
2015-01-02 0
2015-01-03 0
2015-01-04 0
而且有一個表2
datestart dateend value
-------------------------------------
2015-01-02 2015-01-03 1
我想獲得像以下
date value
-------------------------
2015-01-01 0
2015-01-02 1
2015-01-03 1
2015-01-04 0
一個結果我試着用cross apply
select table1.date, temp.value
from table1
cross join
(select table2.value from table2 where
table2.startdate <= table1.date and table2.enddate > table1.date) as temp
,但我最終
date value
-------------------------
2015-01-02 1
2015-01-03 1
什麼是錯在我的代碼?
運行代碼後,我得到了相同的結果我的。我想擁有table1中的所有日期(tally表包含所有365/366天),但是更改後的值取決於table2中的值。 – mko