How to GROUP BY Month and Year in SQLite?
SQLite is a lightweight and server–less relational database management system. It requires very minimal configurations therefore it is easy to integrate into applications. It is also considered as an ideal choice for small mobile and desktop applications. In SQLite, GROUP BY month and year is considered useful when we are dealing with some time–based analysis.
It is also useful when dealing with other tasks like data visualization, forecasting, planning, etc. In this article, We will learn about How to GROUP BY Month and Year in SQLite by understanding the various methods along with the various examples and so on.
How to GROUP BY Month and Year in SQLite
In SQLite, we can easily perform GROUP BY month and year with the help of strftime() function. The strftime() function will help us to extract the month and year from the date entered in the table. Thus, we can perform the GROUP BY related operations accordingly.
Syntax:
SELECT column_name(s)
FROM table_name
GROUP BY STRFTIME(''%m-%Y'', column_name);
Explanation: This query selects columns from a table and groups the results by formatting a column containing dates into a string representing the month and year (for examples: ’03-2024′ for March 2024).
The STRFTIME
(
'%m-%Y', column_name
)
function is used for this formatting. The GROUP BY
clause then groups the results based on this formatted string, allowing for aggregation and analysis based on month and year.
Let’s set up an Environment
To understand How to GROUP BY Month and Year in SQLite we need a table on which we will perform various operations and queries. Here we will consider a table called w3wiki which contains id, name, questions, and submission_day as Columns.
After Inserting some data into the table the table looks:
Output:
Using strftime Function
Example 1: GROUP BY Month
In this example, we are going to perform GROUP BY operation with month. We are going to count number of fields from the table grouping them with month.
In simple words, we are going to calculate number of rows with respect to the month they belong.
Query:
SELECT STRFTIME('%m', submission_day) AS sumission_month, COUNT(*) as month_count
FROM w3wiki
GROUP BY STRFTIME('%m', submission_day);
Output:
Explanation: We can see that all the months have their respective row count which represents the records which contains that month.
Example 2: GROUP BY Year
In this example, we are going to perform the same operation but this time we will group them with year. We are going to count all the records with respect to there years.
Query:
SELECT STRFTIME('%Y', submission_day) AS sumission_year, COUNT(*) as year_count
FROM w3wiki
GROUP BY STRFTIME('%Y', submission_day);
Output:
Explanation: As we have seen in the previous example, all the years have their respective row count value.
Example 3: GROUP BY month along with year
In this example, we are going to group all the records with respect to their month but this time we will focus on their respective year too.
Query:
SELECT STRFTIME('%m-%Y', submission_day) AS sumission_month_year, COUNT(*) as month_year_count
FROM w3wiki
GROUP BY STRFTIME('%m-%Y', submission_day);
Output:
Explanation: In the above image, we can clearly see that month and year has been displayed along with their respective row count value.
This represents the records which are present in that particular month and year. This time we have kept focus on both i.e. month and year.
Conclusion
Overall, to GROUP BY month and year is easily done with strftime() function. The strftime() function will help us to extract month and year from the previously inserted date. We have covered how we can perform GROUP BY related operations with month, year and both combined. Now you have a good understanding on performing GROUP BY operations with month and year. Now you can write queries related to it and can get the desired results.
Contact Us