Statistical data sql writing by year, month and day

Statistical data sql writing by year, month and day

I have recently encountered the problem of statistical calculations for the year, month, and day, and I haven't got a clue for a short time (a few seconds), so I summarize it and record it.
---------- written in front

###### 
1.先看数据库,我随便找了一个有时间的数据库,没仔细去琢磨,将就用。

Insert picture description here

2. Let's first recognize a few functions

uploadtime: 2019-12-08 14:49:30
year (uploadtime) Get the year in the time field, the result is 2019
month (uploadtime) Get the month in the time field, the result is 12
day (uploadtime) Get the day in the time field , The result is 08

3. Statistics by year:
select year(uploadtime) as yy,count(*),sum(filetypeid) from sys_base_fileindex GROUP BY yy

Insert picture description here

4. Statistics by month:
select year(uploadtime) as yy, month(uploadtime) as mm,count(*),sum(filetypeid) from sys_base_fileindex GROUP BY yy,mm

Insert picture description here

5. By daily statistics
select year(uploadtime) as yy, month(uploadtime) as mm,day(uploadtime) as dd ,count(*),sum(filetypeid) from sys_base_fileindex GROUP BY yy,mm,dd

Insert picture description here
Summary:
Here is a guideline, the order of multiple fields of group by:
GROUP BY X means to put all records with the same X field value into a group.
GROUP BY X, Y means put all records with the same X field value and Y field value into a group
GROUP BY X, Y, Z means put all records with the same X field value and Y field value and Z field value Put records in a group

Published 67 original articles · Liked12 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/m0_37635053/article/details/103899068