Grouping statistics statement for SQL data by day and month

Source: Internet
Author: User

Grouping statistics statement for SQL data by day and month

Grouping statistics statement for SQL data by day and month
/*
In the database tutorial orders table, there is a "total" and a "createtime" field. Now we want to get the total of every day of the current month. If there are 31 days in this month, we need to get the total of 31 total items.
*/

$ SQL = "select sum (total), convert (char (10), createtime, 120) createtime
From tb
Group by convert (char (10), createtime, 120)
Order by sum (total) desc

";

$ Sql2 = "select sum (total) total, convert (varchar (7), createtime, 120) as month
From orders
Group by convert (varchar (7), createtime, 120 )";

 

$ Sql3 = "select sum (total) as sum, convert (char (10), createtime, 120) createtime
From tb
Where datepart (month, createtime) = datepart (month, getdate ())
Group by convert (char (10), createtime, 120)
";

$ Sql4 = "select
Convert (datetime, floor (convert (float, createtime), sum (total)
From
Orders
Group
Floor (convert (float, createtime ))
";

/*
The preceding four types of mssql SQL statements have their own advantages in grouping statistics by day and month. You can give it a try.
*/

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.