sql server - Count group by date zero result -
i have sql report:
select count(id_reg) total, year(fechaingreso) ano, month(fechaingreso) mes, day(fechaingreso) dia vw_casos_tc fechaingreso between '2012-12-05' , '2013-5-8' group day(fechaingreso), month(fechaingreso), year(fechaingreso) order ano, mes, dia
it works fine excepts days have no records when count(id_reg) = 0, days doesn't appear on result.
total ano mes dia ----------------------------- 66 2012 12 13 22 2012 12 14 23 2012 12 17
there no result (2012 12 15 , 2012 12 16). how can modify script days total = 0 ?
here way, assuming sql server 2008 (though can modify sql server 2005):
declare @startdate date, @enddate date set @startdate = '20121205' set @enddate = '20130508'; alldays ( select @startdate yourdate union select dateadd(day,1,yourdate) alldays dateadd(day,1,yourdate) <= @enddate ) select a.yourdate, isnull(b.total,0) total alldays left join ( select convert(date,fechaingreso) yourdate, count(id_reg) total vw_casos_tc fechaingreso >= @startdate , fechaingreso < dateadd(day, 1, @enddate) group convert(date,fechaingreso)) b on a.yourdate = b.yourdate option(maxrecursion 0)
Comments
Post a Comment