sql query data from January to December each year

sql is as follows

Select Year(Time),Month(Time),SUM(Number) 
FROM table1
Group by Year(Time),Month(Time)

Note: Time field, Number data field, table1 table name

The result is as follows

 The result contains 2021. If you only want to check the data of this year, add it after the where condition

DATE_FORMAT(Time,'%Y') = DATE_FORMAT(SYSDATE(),'%Y')

 Query the total number of cooked meals per month in this year.  The complete sql is as follows:

Select Year(Time),Month(Time),SUM(Number) 
FROM table1
WHERE DATE_FORMAT(Time,'%Y') = DATE_FORMAT(SYSDATE(),'%Y')
Group by Year(Time),Month(Time)

At this point, only this year's data is available!

Guess you like

Origin blog.csdn.net/qq_61726905/article/details/127324411