Weekly Statistics on SQL Server

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.