I have more than 10,000+ lines/rows of data for a single day.
How can I know which date in a month has max row/lines? I want to find the trend of Max number of breaks every month.
I have more than 10,000+ lines/rows of data for a single day.
How can I know which date in a month has max row/lines? I want to find the trend of Max number of breaks every month.
The problem is that you need to group by day (entries per day) and then group by month (maximum entries per day in a month). You can do that in one query, but I think there's no effective and easy way to achieve that since you can't use derived tables multiple times. Here's what I suggest:
Let's say you have a table like this:
MariaDB [test]> create table dates (date datetime, data int default null);
Query OK, 0 rows affected (0.02 sec)
Put in some rows:
MariaDB [test]> select * from dates;
+---------------------+------+
| date | data |
+---------------------+------+
| 2018-10-03 00:00:00 | NULL |
| 2018-01-01 00:00:00 | NULL |
| 2018-01-02 00:00:00 | NULL |
| 2018-01-02 00:00:00 | NULL |
| 2018-01-02 00:00:00 | NULL |
| 2018-01-03 00:00:00 | NULL |
| 2018-01-03 00:00:00 | NULL |
| 2018-02-01 00:00:00 | NULL |
+---------------------+------+
8 rows in set (0.00 sec)
A sample group by
(entries per day):
MariaDB [test]> select count(*) entries, year(date) y, month(date) m, day(date) d from dates group by y, m, d;
+---------+------+------+------+
| entries | y | m | d |
+---------+------+------+------+
| 1 | 2018 | 1 | 1 |
| 3 | 2018 | 1 | 2 |
| 2 | 2018 | 1 | 3 |
| 1 | 2018 | 2 | 1 |
| 1 | 2018 | 10 | 3 |
+---------+------+------+------+
5 rows in set (0.00 sec)
From here it would get complicated. In order to circumvent that, we create a view:
MariaDB [test]> create view counts_per_day as (select count(*) entries, year(date) y, month(date) m, day(date) d from dates group by y, m, d);
Query OK, 0 rows affected (0.01 sec)
MariaDB [test]> select * From counts_per_day;
+---------+------+------+------+
| entries | y | m | d |
+---------+------+------+------+
| 1 | 2018 | 1 | 1 |
| 3 | 2018 | 1 | 2 |
| 2 | 2018 | 1 | 3 |
| 1 | 2018 | 2 | 1 |
| 1 | 2018 | 10 | 3 |
+---------+------+------+------+
5 rows in set (0.00 sec)
With this view, the next part is much simpler, as it's a typical greatest-n-per-group problem:
MariaDB [test]> select entries, a.y, a.m, a.d from counts_per_day a inner join (select y, m, max(entries) max_entries from counts_per_day group by y, m) b on a.entries = max_entries and a.y = b.y and a.m = b.m;
+---------+------+------+------+
| entries | y | m | d |
+---------+------+------+------+
| 3 | 2018 | 1 | 2 |
| 1 | 2018 | 2 | 1 |
| 1 | 2018 | 10 | 3 |
+---------+------+------+------+
3 rows in set (0.00 sec)