Your outer joins are confused.Also:1. Use a number table instead of recursion. (Google number/tally table)2. Do not do needless casts.Depending on your view of RIGHT JOINs vs nested JOINS:eitherDECLARE @Start datetime = '20130502', @End datetime = '20130510';SELECT @Start + N.number AS [Date] ,DATENAME(dw, @Start + N.number) AS [DayName] ,COALESCE(SUM(D.DetailQuantity), 0) AS QTYFROM MasterTable M JOIN DetailsTable D ON M.ID = D.ID RIGHT JOIN [master].dbo.spt_values N ON N.[type] = 'P' AND N.number <= DATEDIFF(d, @Start, @End) AND DATEADD(d, DATEDIFF(d, 0, M.MDate), 0) = @Start + N.number;
orDECLARE @Start datetime = '20130502', @End datetime = '20130510';SELECT @Start + N.number AS [Date] ,DATENAME(dw, @Start + N.number) AS [DayName] ,COALESCE(SUM(D.DetailQuantity), 0) AS QTYFROM [master].dbo.spt_values N LEFT JOIN ( MasterTable M JOIN DetailsTable D ON M.ID = D.ID ) ON @Start + N.number = DATEADD(d, DATEDIFF(d, 0, M.MDate), 0)WHERE N.[type] = 'P' AND N.number <= DATEDIFF(d, @Start, @End);