Why the following SQL Server query returns 12 months data and not 14 months data -


i have following query returns data 12 months. query 14 days , changed day month in datediff method 14 months data getting 12 months data. can please check , see why?

select 'playing' activity  --,ad.xdate  ,min(ad.xdate) xdate ,isnull(sum(t.timeperday),0) timeperday     alldates ad (nolock)  left join @test t on ad.xdate = t.date group datepart(month, ad.xdate) --order year(datepart(month, ad.xdate)) desc, month(datepart(month, ad.xdate)) desc, day(datepart(month, ad.xdate)) order min(ad.xdate) option (maxrecursion 0) end 

you need group both month , year parts of ad.xdate. there twelve months of year , should seeing earliest 2 (14 - 2) month of results totals large because represent combination of 2 calendar months.

it worked in original version because there more 14 days in month. if tried extend old query beyond 31 days (or 28, 29, 30 months) find same problem on again.

... select     'playing' activity,     min(ad.xdate) xdate,      isnull(sum(t.timeperday), 0) timeperday alldates ad left outer join @test t on ad.xdate = t.date group year(ad.xdate), month(ad.xdate) /* <--- change here */ order xdate 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -