How to get the count of day with most rows with MySQL - mysql

I have a table with the following rows. id, address, timestamp. Timestamp is a unix timestamp. How can I get the day that has the most rows, and the number of rows in that day using 1 query. I am using MySQL 5.6.34.

Group the rows by date, use COUNT(*) to get the count of rows in each group, and then use ORDER BY and LIMIT to get the highest count.
SELECT DATE(timestampColumn) AS date, COUNT(*) AS count
FROM yourTable
GROUP BY date
ORDER BY count DESC
LIMIT 1

Related

Select all rows with the same aggregated value

There is a task: develop a fragment of the Web site that provides work with one table.
Attributes of the table:
Day of the week,
Time of the beginning of the lesson,
Subject name,
Number of the audience,
Full name of the teacher.
We need to make a query: determine the day of the week with the largest number of entries, if there are more than one maximum (ie, they are the same), then output them all. I did the query as follows:
SELECT COUNT (*) cnt, day
FROM schedule
GROUP BY day
ORDER BY cnt DESC
LIMIT 1;
But if there are several identical maxima, then only one is displayed. How to write a query which returns them all?
You can use your query as a subquery in the HAVING clause, e.g.:
SELECT day, count(*) as cnt
FROM schedule
GROUP BY day
HAVING count(*) = (
SELECT count(*) as cnt
FROM schedule
GROUP BY day
ORDER BY cnt DESC
LIMIT 1
)
ORDER BY day

Count and group by day

I have a very basic table, consisting of an integer column and a timestamp column.
What's the query to count how many entries there are for each day?
When I use SELECT COUNT(*) FROM taps GROUP BY(DATE(time_stamp)) , I get the total number of rows int he table, rather than the number of rows for each DISTINCT date.
How do I need to modify the query?
Pretty straightforward.
SELECT
DATE(time_stamp),
COUNT(1)
FROM taps
GROUP BY DATE(time_stamp)

Mysql counting records per day with the date next to the count

Hi i am trying to get the count of records per day which i can do, but i also want the date to be show, for example,
Result
Date | Count
26/01/2015 20
25/01/2015 | 413
Here is an example of my data.
I would think this would work. Replace 'yourTable' with your table name
SELECT Date, COUNT(*) FROM yourTable GROUP BY Date;
Get the total count and group them by date.
SELECT `date`, COUNT(*) as Total
FROM `table`
GROUP BY `date`
ORDER BY `date`;

Limiting records number of days and count

Let's say I have a table in MySQL DB with following columns
employee, status, work, start_date
Consider that start_date column is date and time.
If I do
SELECT employee, status, work, start_date from table_name WHERE DATE(date) >= CURDATE()-10
this will give me records from Current date - 10 days. In this case I might get 1 record to 100 records based on the data.
I need only 10 records based on date/time (e.g. if there are 10 employees that started to work today then I should get only today's records and not 10 days record)
How can I do that?
You mean you want the ten most recent entries? You can add an ORDER BY to set the order in which the results come back, and a LIMIT to reduce the total number of results.
SELECT employee, status, work, start_date from table_name
WHERE DATE(date) >= CURDATE()-10
ORDER BY date DESC
LIMIT 10
you need to use order by on start_date and limit
SELECT employee, status, work, start_date
from table_name
order by start_date desc
limit 10

Return the values of max count of rows with the same values

I have table_schedule(id,instructorNumber,time,date,cpNumber,user_id);
Is it possible to output the values of instructorNumber,time, etc. with the same highest occurence with same values?
sorry I am new in sql, so I am hoping that someone can help me to the query
Just group by all the fields you need for "same value comparison", order by count desc (so the result with most occurences will be first), and take first.
select
instructorNumber, time, date, cpNumber
from table_schedule
group by instructorNumber, time, date, cpNumber
order by count(*) desc
LIMIT 1
you may use this as a join on a main query if you want more than one result.
First group by the values you want to compare by and count. Get the maximum count (in MySQL you can use LIMIT for that). Then do the same group-by query again and take only the results HAVING the maximum count. Use GROUP_CONCAT to get a list of IDs in a string.
select instructorNumber, time, date, cpNumber, user_id, group_concat(id) as ids
from table_schedule
group by instructorNumber, time, date, cpNumber, user_id
having count(*) =
(
select count(*)
from table_schedule
group by instructorNumber, time, date, cpNumber, user_id
order by count(*) desc limit 1
);