I am making a report today, but I did not pay attention to it at first glance. I checked it carefully. It turns out that I want to count the sales volume for a certain period of time in a sales detail list in SQL Server and compare it by week, I had no idea at all in my mind. I thought I would never have to go through a loop to perform computation. I checked the information and found that I could use a short SQL statement, it seems that SQL Server's basic skills are still not solid. The statement is written as follows:
Select sum (sales amount), datename (Week, sales date-1) from sales where sales date betwee begindate and enddate group by datename (Week, sales date-1)
Note: here we need to set the sales date-1 because the first day of the week in SQL Server is Sunday by default, and the statistics we use are calculated from Monday to Sunday.