當然可以。這從輸入設定生成天,然後給你你需要
儘管這在技術上內部就像是臨時表,你可以創建一個遞歸視圖範圍:
Create View TestView as
with Data As -- Pretends to be your table of data replace with select from your tables
(
select Cast('2012-05-01' as DATETIME) [Start Date], Cast('2012-05-02' as DATETIME) [End Date], 2 [Avgerage Value Per Day]
union all
select Cast('2012-04-01' as DATETIME) [Start Date], Cast('2012-04-05' as DATETIME) [End Date], 3 [Avgerage Value Per Day]
)
,AllDates as -- generates all days
(
select cast('1900-01-01' as datetime) TheDate
union all
select TheDate + 1
from AllDates
where TheDate + 1 < '2050-12-31'
)
select TheDate [Date], o.[Avgerage Value Per Day]
from AllDates
join Data o on TheDate Between o.[Start Date] AND o.[End Date];
可以查詢,但您需要確保您指定的遞歸限制
select * from TestView
OPTION (MAXRECURSION 0)
這給出了這樣的結果
Date Avgerage Value Per Day
2012-04-01 00:00:00.000 3
2012-04-02 00:00:00.000 3
2012-04-03 00:00:00.000 3
2012-04-04 00:00:00.000 3
2012-04-05 00:00:00.000 3
2012-05-01 00:00:00.000 2
2012-05-02 00:00:00.000 2
您可以從測試數據中看到我想要的5月1 - 2日和4月1日至5日
看起來像一個剛剛問過的問題:http:// stackoverflow。com/questions/10532462/create-a-select-statement-with-group-by/10532543#10532543 – amaters