Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group by day using the timestamp field

Tags:

mysql

My table schema looks like this:

+--------------------+--------------+------+-----+-------------------+-----------------------------+
| Field              | Type         | Null | Key | Default           | Extra                       |
+--------------------+--------------+------+-----+-------------------+-----------------------------+
| id                 | int(11)      | NO   | PRI | NULL              | auto_increment              |
| name               | varchar(50   | NO   |     | 0                 |                             |
| modified           | timestamp    | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| created            | timestamp    | NO   |     | CURRENT_TIMESTAMP |                             |
+--------------------+--------------+------+-----+-------------------+-----------------------------+

I want to get a count of the names and group by number of names modified by day at the moment I can only group by the full date including the timestamp eg:

SELECT name, count(*) FROM mytable GROUP BY modified

Thanks in advance.

like image 992
James MV Avatar asked Jun 11 '18 11:06

James MV


People also ask

How do I Group A timestamp by month?

You can group month and year with the help of function DATE_FORMAT() in MySQL. The GROUP BY clause is also used.

Can we GROUP BY date in SQL?

1 Answer. You can use DATE_FORMAT operator. If you are using this you can easily group the date, timestamp or datetime column using whatever format you want.

How do I pick a date from a timestamp?

In MySQL, use the DATE() function to retrieve the date from a datetime or timestamp value. This function takes only one argument – either an expression which returns a date/datetime/ timestamp value or the name of a timestamp/datetime column.

Can count be used with GROUP BY?

The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country". The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns.

How do I Group by timestamp in SQL Server?

in SQL Server. When you want to group by minute, hour, day, week, etc., you may be tempted to just group by your timestamp column. If you do that, though, you'll get one group per second -- likely not what you want. Instead, you should “truncate” your timestamp to the granularity you want, like minute, hour, day, week, etc.

What is the syntax of group by day in PostgreSQL?

Below is the parameter description syntax of group by day in PostgreSQL: Select: Select operation is used to select the data from the table and convert it into day wise by using the date_trunc and to_char function in PostgreSQL. We have selected the date column from the table to convert data as per day wise.

How do I Group by the date?

If you want to group by the entire date (as opposed to a particular day), ignoring the time you can use The full list of format specifiers can be found at the above link, but what you'll probably need is: This will format the date as 'YYYY-MM-DD' and you can group by that.

How to convert date column to day Wise in PostgreSQL?

Select: Select operation is used to select the data from the table and convert it into day wise by using the date_trunc and to_char function in PostgreSQL. We have selected the date column from the table to convert data as per day wise. Date_trunc: This function is used to convert the date column data as per day wise by using the group by day.


3 Answers

Use MySQL DATE() function to extract the date from the timestamp:

SELECT name, count(*) FROM mytable GROUP BY DATE(mytable.modified);

The DATE() function extracts the date value from a date or datetime expression.

like image 148
UkFLSUI Avatar answered Nov 02 '22 23:11

UkFLSUI


Dependant upon what you mean by day, you have a few options; Here's your cheat sheet.

DAYNAME(date) for the day of the week (Mon-Sun)

DAYOFMONTH(date) for the day of the month (1-31)

DAYOFWEEK(date) for the day of the week (1-7)

DAYOFYEAR(date) for the day of the year (1-365)

Edit:

If you want to group by the entire date (as opposed to a particular day), ignoring the time you can use

DATE_FORMAT(date, format)

The full list of format specifiers can be found at the above link, but what you'll probably need is:

Date_FORMAT(date, '%Y-%m-%d')

This will format the date as 'YYYY-MM-DD' and you can group by that.

like image 38
Will Jones Avatar answered Nov 02 '22 23:11

Will Jones


You can use this query or a version of it SELECT DATE_FORMAT(modified,"%Y-%m-%d") as date_string, count(1) FROM mytable group by date_string;

like image 25
muraguri2005 Avatar answered Nov 03 '22 00:11

muraguri2005