Counting date time stamp occurrences - mysql

I need to count how many datetimes stamps occur within each hour.
Say I have this timestamp 2012-03-22 15:33:56.0 and 2012-03-30 08:22:45.0 in a column called date_time.
How do I put the hour (15 and 8) and count how many times those timestamps occurred (1 and 1) into separate columns!
Anything helps!

Please see the following DBFiddle, you can use the HOUR() function on the date time column and aggregate results : https://www.db-fiddle.com/f/2eKrampo67wNSnCajmwTok/0

Related

How many Intervals are in a period?

Given a table with two dates and a date interval (i.e. 6 WEEKS), how can I count how many times this interval fits in this date interval?
In my PHP script I'm using a simple for loop, but I'd like to add this in a MySQL query.
A simple calculation would be to count the number of days between the dates and then assume a month has 30 days and a year has 365 days, but that's not always true.
How can I calculate with datediff and a date interval string?
When I understood you correctly, you need the timestampdiff() function.
The first parameter is the unit of the interval. The result is how many units are between the two dates.

SQL how can i get previous minute?

I have a data table whose structure looks like this.
date time order_id action quantity
How can I query to know how many entries are made to this table previous minute?
Say suppose the time now is 14:46 I want to know how many row entries were made to this table at 14:45. how can I do that?
The problem I am facing now is that I don't know how can I get last minute's time stamp. CURRENT_TIMESTAMP() is giving me correct current time. But I tried CURRENT_TIMESTAMP()-1 which gives some decimal number.
you can use DATE_SUB(), specifying a date and an INTERVAL as documented for DATE_ADD(). Example:
SELECT DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 MINUTE);
You can use my query to find the last minute :
SELECT DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 MINUTE);

mysql - get average of the difference between two data cells

I have a table of MySQL data. Each row has its own data. In that row there is start_time and end_time. Basically, when you started doing an objective and when you finished (inserted into the database). Like a timer of sorts.
How would I get the average of of taking the unix timestamp of start_time and end_time. I know you would minus the end_time by start_time to get the difference (in milliseconds?) and from there... not sure what else.
unix_timestamp of a date column returns its representation as seconds from the epoc, so subtracting two of these will give a difference in seconds. Like any other number, you can apply the aggregate avg function to it in order to get an average:
SELECT AVG (UNIX_TIMESTAMP(end_time) - UNIX_TIMESTAMP(start_time))
FROM my_table
Once you have this result, you could manipulate it in any way you like. One useful manipulation would be to use sec_to_time to convert a number of seconds to a HH:MM:SS format (e.g., 183 seconds would be represented as 00:03:03 hours):
SELECT SEC_TO_TIME
(AVG (UNIX_TIMESTAMP(end_time) - UNIX_TIMESTAMP(start_time)))
FROM my_table

Fetch mysql data of timstamp depending on time given

I have a column with timestamp, contain example value "2014-04-16 18:00:00","2014-04-17 18:00:00"....
Now, if I will call a page before "2014-04-17 12:00:00" I need this value-"2014-04-16 18:00:00"
And if I call my page after "2014-04-17 12:00:00" I need this value "2014-04-17 18:00:00".
I think my question is very complicated to understand, having complications in date & times, please check date & time properly.
I want to fetch this data from DB in mysql, The page I was saying is that where I'm going to add your mysql query.
Thanx in advance
Generalising what your asking for a bit the following will return dates from the previous day if it's before noon and dates from today if it's after noon:
SELECT date_column
FROM yourTable
WHERE DATE(DATE_SUB(NOW(), INTERVAL 12 HOUR)) = DATE(date_column);
Edit:
The WHERE clause First gets the current time (NOW()) and subtracts 12 hours. This wont affect the date unless the time is before 12. This means DATE_SUB(NOW(), INTERVAL 12 HOUR) gives us today if it's after noon and yesterday if it's before.
We then check if the date_column matches the date we've created (using the DATE function so that the time is ignored).
Adding some rows to the SELECT may help you see how these dates are built up.

How do I subtract using SQL in MYSQL between two date time values and retrieve the result in minutes or second?

I want to subtract between two date time values using SQL in MySQL such that I get the interval in minutes or seconds. Any ideas? I want to run a SQL query that retrieves uses from a database who have logged in like 10 minutes from the time.
There are functions TIMEDIFF(expr1,expr2), which returns the value of expr1-expr2, and TIME_TO_SEC(expr3), which expresses expr3 in seconds.
Note that expr1 and expr2 are datetime values, and expr3 is a time value only.
Check this link for more info.
TIMESTAMPDIFF is like TIMEDIFF which Matthew states, except it returns the difference of the two datetimes in whatever units you desire (seconds, minutes, hours, etc).
For example,
SELECT TIMESTAMPDIFF(MINUTE,LogOutTime,LogInTime) AS TimeLoggedIn
FROM LogTable
Would return the number of minutes the user was logged in (assuming you stored this kind of thing in a table like that).
I would do it like this - fetch where last activity is within 10 mins of now
SELECT * FROM table WHERE last_activity >= DATE_SUB(NOW(), INTERVAL 10 MINUTE)
SELECT TIMESTAMPDIFF(MINUTE,LogOutTime,LogInTime) AS TimeLoggedIn
FROM LogTable
This example shall ruin the time if its used by using millitary time. So for calculating millitairy time I do not recommend it Because it outputs negative values.
You can try and cast them to Unix Time stamp, and take the difference.