sql server 2008 - Select all days from a table missing some dates -
i have table data below, , i'm trying sum column b , group day. works great. days have no data in table @ all. want show these days having sum 0, i'm bit confused on how there.
date, column b 05/24/90, 5 05/24/90, 27 05/26/90, 19 05/27/90, 24
what want have in end is
05/24/90, 32 05/25/90, 0 05/25/90, 19 05/27/90, 24
etc...
something should work:
declare @mindate date = (select min([date]) yourtable); declare @maxdate date = (select max([date]) yourtable); cte_dates ( select @mindate dates union select dateadd(day,1,dates) cte_dates dates < @maxdate ) select a.dates, sum(isnull(b.[column b],0)) cte_dates left join yourtable on a.dates = b.dates group a.dates
Comments
Post a Comment