How to avoid datepart to start from new week on ye

2019-08-07 14:51发布

问题:

I have an sql query which I am using to get weekly data. It works fine but when the year changes it stops the week on 4 days or whatever and starts a new week for new year. I dont want that. I want it to keep working like like before. Any idea of how to force GROUP BY datepart(week, ad.xDate)

Select 'playing' As activity 
,min(ad.xDate) As xDate
,Isnull(sum(t.TimePerDay),0) As TimePerDay 
From    AllDates As ad With (Nolock) 
Left Join @test As t On ad.xDate = t.date
GROUP BY datepart(week, ad.xDate)   
ORDER BY MIN(ad.xDate)
option (maxrecursion 0)

回答1:

GROUP BY
    DATEDIFF(week, 0, ad.xDate)