這會給你日期的表格,你可以OUTER JOIN
與您的數據:
declare @Start as Date = '20130501';
declare @End as Date = '20130515';
with Dates as (
select @Start as [ReportDate]
union all
select DateAdd(day, 1, ReportDate)
from Dates
where ReportDate < @End)
select ReportDate
from Dates option (MaxRecursion 0);
編輯:或者,帶樣品數據:
declare @Production as Table (ActivityDate Date, ProductionQuantity Int);
insert into @Production (ActivityDate, ProductionQuantity) values
('20130106', 400),
('20130112', 550),
('20130112', 50);
declare @Start as Date = '20130101';
declare @End as Date = '20130115';
with Dates as (
select @Start as [ReportDate]
union all
select DateAdd(day, 1, ReportDate)
from Dates
where ReportDate < @End)
select ReportDate, Coalesce(Sum(P.ProductionQuantity), 0) as Qty
from Dates as D left outer join
@Production as P on P.ActivityDate = D.ReportDate
group by D.ReportDate
option (MaxRecursion 0);
什麼數據類型是'日期'列? –
另外,作爲一個數據庫正常化的事情,你不應該存儲星期幾。 –
您是否問如何在範圍內爲_every_日期生成輸出,即使該表不包含某些日期的數據? – HABO