MySQL query to group results by date and display the count of duplicate values?

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable1496
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> PassengerCode varchar(20),
   -> ArrivalDate datetime
   -> );
Query OK, 0 rows affected (0.85 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1496(PassengerCode,ArrivalDate) values('202','2013-03-12 10:12:34');
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable1496(PassengerCode,ArrivalDate) values('202_John','2013-03-12 11:00:00');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable1496(PassengerCode,ArrivalDate) values('204','2013-03-12 10:12:34');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable1496(PassengerCode,ArrivalDate) values('208','2013-03-14 11:10:00');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable1496(PassengerCode,ArrivalDate) values('202','2013-03-18 12:00:34');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable1496(PassengerCode,ArrivalDate) values('202','2013-03-18 04:10:01');
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1496;

This will produce the following output −

+----+---------------+---------------------+
| Id | PassengerCode | ArrivalDate         |
+----+---------------+---------------------+
|  1 | 202           | 2013-03-12 10:12:34 |
|  2 | 202_John      | 2013-03-12 11:00:00 |
|  3 | 204           | 2013-03-12 10:12:34 |
|  4 | 208           | 2013-03-14 11:10:00 |
|  5 | 202           | 2013-03-18 12:00:34 |
|  6 | 202           | 2013-03-18 04:10:01 |
+----+---------------+---------------------+
6 rows in set (0.00 sec)

Here is the query to group results by date and display the count of duplicate values −

mysql> select date(ArrivalDate),count(ArrivalDate) from DemoTable1496
   -> where PassengerCode like concat('%','202','%')
   -> group by date(ArrivalDate);

This will produce the following output −

+-------------------+--------------------+
| date(ArrivalDate) | count(ArrivalDate) |
+-------------------+--------------------+
| 2013-03-12        |                  2 |
| 2013-03-18        |                  2 |
+-------------------+--------------------+
2 rows in set (0.00 sec)
raja
Published on 11-Dec-2019 05:39:29
Advertisements