How to implement a no time period in the use of the zeros fill??
if object_id('[A]') is not NULL Drop Table [A]Go Create Table [A]([Date] datetime,[Amount] int)Insert [A]Select * from [A]Select '2014-05-01', - Union AllSelect '2014-05-02', $ Union AllSelect '2014-05-05', - Union AllSelect '2014-05-06', $Declare @stdate smalldatetime, @eddate smalldatetimeSelect @stdate=min([Date]), @eddate=Max([Date]) from [A]; withSel as( Select @stdate as [Date],1 asValUnion All Select DateAdd( Day, Val,@stdate), Val+1 asVal fromSelwhereVal<=DateDiff( Day,@stdate,@eddate))SelectSel.[Date] as [Date],sum(IsNullA.[Amount],0)) as [Amount] fromsel Left Join [A] onSel.[Date]=[A].[Date] Group bySel.[Date]
Explain: With this step is key, through which it makes "select DATEADD (Day,val, @stdate), val+1 as Val from Sel where Val<=datediff (day, @stdate, @ Eddate) "has been querying the time difference
Then associate through an outer link
Data that is not in the SQL time period equals 0