I have a table with a column called timestamp (YYYY-MM-DD HH:MM:SS) and message. How would I display how many messages where sent every minute (not just in an given time).
I've done this but it's for every time. How would I go about changing it
to count the number sent from HH-00:00 to HH:01:00 and so on.
SELECT timestamp, COUNT(*)
FROM Messages
GROUP BY timestamp
ORDER BY timestamp DESC
SELECT TIMESTAMP(DATE(timestamp),MAKETIME(HOUR(timestamp),MINUTE(timestamp),0))
,COUNT(1)
FROM
Messages
GROUP BY TIMESTAMP(DATE(timestamp),MAKETIME(HOUR(timestamp),MINUTE(timestamp),0))
ORDER BY TIMESTAMP(DATE(timestamp),MAKETIME(HOUR(timestamp),MINUTE(timestamp),0))
Try:
SELECT HOUR(timestamp) AS Hour,
MINUTE(timestamp) AS minute,
COUNT(*)
FROM Messages
GROUP BY HOUR(timestamp), MINUTE(timestamp)
ORDER BY HOUR(timestamp) DESC, MINUTE(timestamp) DESC
Related
I have a list of data contians (yyyy-mm-dd, hh:min:ss) from this data i need to sort out how many times 'logins' was punched for any given hour of the day. and also how many times logins' was punched for each day of the week .
I tried some code but am not confident the code i have right. Also should i include COUNT(*) in the statement
This code for hourly logins :
SELECT date_time, HOUR(date_time) FROM time_logs ORDER BY DAY(date_time);
Code for day for week:
SELECT date_time, DAY(date_time) FROM time_logs ORDER BY DAY(date_time);
Is this is right. If not could you give me hint
To count how many logins per hour, you just need to add COUNT(*) in your SELECT and GROUP BY HOUR(date_time).
SELECT date_time, HOUR(date_time),COUNT(*) 'Total Logins'
FROM time_logs GROUP BY HOUR(date_time) ORDER BY DAY(date_time);
Similarly on your day count you need to add the same thing. Only difference is your grouping is now by day.
SELECT date_time, DAY(date_time), COUNT(*) 'Total Logins'
FROM time_logs GROUP BY DAY(date_time) ORDER BY DAY(date_time);
You can cast your datetime to both hour and date and then group by to get your aggregations.
https://rextester.com/BQSV80644
For hour of the day:
SELECT HOUR(date_time), COUNT(*)
FROM time_logs
GROUP BY HOUR(date_time);
For day of the week:
SELECT WEEKDAY(date_time), COUNT(*)
FROM time_logs
GROUP BY WEEKDAY(date_time)
ORDER BY MIN(date_time)
I have a table named "loginhistory", and I need to calculate the most active hour of the month. How can i do it?
My table structure is:
id, userId, date (datetime), ip.
I tried to do it in PHP but I did not succeed there either. I prefer the calculation to be done only in MySql.
I expect the output to be just the number of the most active hour.
It does not matter checking average number if needed to pick active hour.
Cause average = count / days and since days is the same for whole calculation (let's say 30) the greatest count will be active hour in list.
Just select by date range and group by hour and pick 1st one from descending sort:
SELECT
HOUR(date) AS hour,
COUNT(id) AS logins,
( COUNT(id) / DAY(LAST_DAY('2019-01-01')) ) AS logins_avg
FROM loginhistory
WHERE
date >= '2019-01-01 00:00:00' AND date < '2019-02-01 00:00:00'
GROUP BY HOUR(date)
ORDER BY logins DESC LIMIT 1
In table are two columns: Datestamp like 2012-12-16 with type date and Timestamp like 12:22:59 with type time. Is it possible to get count of rows for each hour for specific day with 1 sql? On return i need to get 24 numbers.
Something like this:
SELECT COUNT(*) FROM your_table
WHERE `Datestamp` = '2012-12-16'
GROUP BY HOUR(`Timestamp`)
SELECT datestamp as day, hour(timestamp) as hour, count(*)
FROM your_table
GROUP BY datestamp, hour(timestamp)
I have a number of posts saved into a InnoDB table on MySQL. The table has the columns "id", "date", "user", "content". I wanted to make some statistic graphs, so I ended up using the following query to get the amount of posts per hour of yesterday:
SELECT HOUR(FROM_UNIXTIME(`date`)) AS `hour`, COUNT(date) from fb_posts
WHERE DATE(FROM_UNIXTIME(`date`)) = CURDATE() - INTERVAL 1 DAY GROUP BY hour
This outputs the following data:
I can edit this query to get any day I want. But what I want now is the AVERAGE of each hour of every day, so that if on Day 1 at 00 hours I have 20 posts and on Day 2 at 00 hours I have 40, I want the output to be "30". I'd like to be able to pick date periods as well if it's possible.
Thanks in advance!
You can use a sub-query to group the data by day/hour, then take the average by hour across the sub-query.
Here's an example to give you the average count by hour for the past 7 days:
select the_hour,avg(the_count)
from
(
select date(from_unixtime(`date`)) as the_day,
hour(from_unixtime(`date`)) as the_hour,
count(*) as the_count
from fb_posts
where `date` >= unix_timestamp(current_date() - interval 7 day)
and created_on < unix_timestamp(current_date())
group by the_day,the_hour
) s
group by the_hour
Aggregate the information by date and hour, and then take the average by hour:
select hour, avg(numposts)
from (SELECT date(`date`) as day, HOUR(FROM_UNIXTIME(`date`)) AS `hour`,
count(*) as numposts
from fb_posts
WHERE DATE(FROM_UNIXTIME(`date`)) between <date1> and <date2>
GROUP BY date(`date`), hour
) d
group by hour
order by 1
By the way, I prefer including the explicit order by, since most databases do not order the results of a group by. Mysql happens to be one database that does.
SELECT
HOUR(FROM_UNIXTIME(`date`)) AS `hour`
, COUNT(`id`) \ COUNT(DISTINCT TO_DAYS(`date`)) AS avgHourlyPostCount
FROM fb_posts
WHERE `date` > '2012-01-01' -- your optional date criteria
GROUP BY hour
This gives you a count of all the posts, divided by the number of days, by hour.
Lets say I have a table of messages that users have sent, each with a timestamp.
I want to make a query that will tell me (historically) the most number of messages a user ever sent in an hour.
So in other words, in any given 1 hour period, what was the most number of messages sent.
Any ideas?
Assuming timestamp to be a DATETIME - otherwise, use FROM_UNIXTIME to convert to a DATETIME...
For a [rolling] count within the last hour:
SELECT COUNT(*) AS cnt
FROM MESSAGES m
WHERE m.timestamp BETWEEN DATE_SUB(NOW(), INTERVAL 1 HOUR)
AND NOW()
GROUP BY m.user
ORDER BY cnt DESC
LIMIT 1
If you want a specific hour, specify the hour:
SELECT COUNT(*) AS cnt
FROM MESSAGES m
WHERE m.timestamp BETWEEN '2011-06-06 14:00:00'
AND '2011-06-06 15:00:00'
GROUP BY m.user
ORDER BY cnt DESC
LIMIT 1
Need more details on table structure etc. but something like:
select date(timestmp), hour(timestmp) , count(*)
from yourtable group by date(timestmp) , hour(timestmp)
order by count(*) DESC
limit 100;
would give you hte desired result.
Something like this should work:
SELECT MAX(PerHr) FROM
(SELECT COUNT(*) AS PerHr FROM messages WHERE msg_uid=?
GROUP BY msg_time/3600) t
I suspect this would be horribly slow, but for an arbitrary historical max hour, something like this might work (downvote me if I'm way off, I'm not a MySQL person):
SELECT base.user, base.time, COUNT(later.time)
FROM messages base
INNER JOIN messages later ON later.time BETWEEN base.time AND DATE_ADD(base.time, INTERVAL 1 HOUR) AND base.user = later.user
WHERE base.user = --{This query will only work for one user}
GROUP BY base.user, base.time
ORDER BY COUNT(later.time) DESC
LIMIT 1