Updated March 10, 2023
Introduction to SQL GROUP BY DAY
SQL GROUP BY DAY is used to get the grouped data in a summarized way based on each of the day in SQL tables. In scenarios, where we require day-wise data of the organization, sales, submissions, tests, etc we can make the use of group by clause and define the grouping criteria as the day record value present in the table records or simply retrieve the day value using functions in SQL from the date and time data typed column values to group the result based on the day. Here we will see about the syntax of grouping the result set of the query statement using SQL group by day, it’s working, and multiple examples to retrieve the column value from the resultset to apply it as grouping criteria.
Syntax of GROUP BY DAY
The syntax of the GROUP BY DAY clause is as follows:
SELECT
column1, column2,..., columnm, aggregate_function(columni)
FROM
target_table
WHERE
conditions_or_constraints
GROUP BY expressionDerivingDayOfColumn ;
The syntax of the GROUP BY clause is as shown above. It is the optional clause used in the select clause whenever we need to summarize and reduce the resultset. It should always be placed after the FROM and WHERE clause in the SELECT clause.
Some of the terms used in the above syntax are given below:
- column1, column2,…, columnm: These are the names of the columns of the target_table table that need to retrieved and fetched in the resultset.
- aggregate_function(columni): These are the aggregate functions defined on the columns of target_table that needs to be retrieved from the SELECT query.
- target_table: Name of the table from where the result is to be fetched.
- conditions_or_constraints: If you want to apply certain conditions on certain columns they can be mentioned in the optional WHERE clause.
- expressionDerivingDayOfColumn: This is the column that will be considered as the criteria to create the groups in the MySQL query based on day value. There can be single or multiple column names on which the criteria need to be applied. We can even mention expressions as the grouping criteria that can include functions available in SQL to retrieve day from the date and time-related data types. It can also be a single column name that stores day value for the records of table.
Example of SQL GROUP BY DAY
Given below is the example:
Consider a table named educba_ articles having the contents and structure as shown in the output of the following select query statement.
Code:
SELECT * FROM educba_articles;
The output of the execution of the above query statement is as follows showing the structure and contents of educba_articles table:
Output:
Now, we will group the resultset of the educba_articles table contents based on a column named assigned_date from which the day value will be obtained so that the retrieved records will only a single record for the rows having the same values for day and data will be seen in a day-wise format. For retrieving the day value from the date of the assignment we will use the SQL DAY() function. Also, we will retrieve the average rate per day and the name of the day from the query statement.
Code:
SELECT
AVG(a.` rate`), DAY(a.` assigned_date`)
FROM
educba_articles a
GROUP BY DAY( a.` assigned_date`);
Output:
We can observe that the output contains all four days and the result set has been grouped day-wise and the rate value that is retrieved is the average rate for each of the days.
Now, we will retrieve the records of the educba_articles table who have status as submitted and the retrieved records should contain the sum of the rate column for each of the day and the name of the day. For this, we will first need to restrict the where clause statement by applying the condition to retrieve only submitted articles and further group the records based on the day of assigned_date column and apply SUM() function on rate column to retrieve the total rate for each of the days for submitted articles.
Code:
SELECT
SUM(a.` rate`), DAY (a.`assigned_date`)
FROM
educba_articles a
WHERE ` status` = "Submitted"
GROUP BY DAY (a.`assigned_date`);
The output of the execution of the above query statement is as follows which does not include the record for date 8 as table records do not have any submitted articles for that date.
Output:
Now, consider a table named educba_experts having the contents and structure as shown in the output of the following select query statement.
Code:
SELECT * FROM educba_experts;
The output of the execution of the above query statement is as follows showing the structure and contents of educba_experts table:
Now, we want to group the rate column based on the day of joining each of the experts. To get the day-wise data we will first need to retrieve the day from the joining date and time column and apply that value as the grouping criteria. Further, we want to retrieve the sum of the rate in a day-wise format. Hence, we will use the aggregate function named SUM() to get the total value of the rate column.
Code:
SELECT
SUM(rate),DAY(joining_date_time)
FROM
`educba_experts`
GROUP BY DAY(joining_date_time );
The output of the execution of the above query statement is as follows showing the days total rate in educba_experts table:
From the above output, we can observe that the total rate of each day is retrieved. You can cross-check by going the total rate column for each day. For example, the experts named vyankatesh and Omprakash with id 19 and 21 joined in the twenty-sixth day of 2020 irrespective of the month in which they joined and hence while grouping their rates will lead to a total of 560.00 and 980.56 that is equivalent to 1540.56 value. Similarly, we can confirm for other days.
Conclusion
We can retrieve the data in a day’s format by grouping the resultset based on the day. For this, some of the columns of the table from which the data is being required must contain the day value or date or date-time value from where the day can be retrieved. Day value can be retrieved from the date-time data typed column using the SQL function DAY(). This is done mostly for building queries for reports.
Recommended Articles
We hope that this EDUCBA information on “SQL GROUP BY DAY” was beneficial to you. You can view EDUCBA’s recommended articles for more information.