这将为您提供一个日期表,您可以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 );