Skip to content
Advertisement

How to group data by past few weeks?

I have an original table like this,

Can I group my data using GROUP BY in SQL to get the aggregate value by each week?

My expected output will be like,

Advertisement

Answer

This is a way of doing it, probably not the best way however. Personally, I feel like this list is not something you’d want to do in SSMS. But rather inside Power BI, Excel or some other software for visualizing data rather than retreiving. Do note that my example below is NOT dynamic in the sense of having more and more weeks. This specifically only works for the 3 weeks specified. Please refer to the documentation to see which date belongs to what week. This is from SSMS v18.7.1

Anyway, here goes:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement