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.
*/