How to calculate times in SQL - mysql

I have a mysql table that has a column named time. How to calculate times in that column using SQL. Times are in HH:MM:SS format.
time
04:00:00
00:25:38
04:32:58
01:12:23
00:00:00
So the total time is 10:10:59. So how do i get this answer using SQL?

See this sqlfiddle:
SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( `time` ) ) )
FROM `times`

You can use TIME_TO_SEC to convert your value into seconds, SUM the seconds up, and then use SEC_TO_TIME to get back into the time form
SELECT sec_to_time( sum( time_to_sec( `time` ) ) ) AS total_time
FROM my_table

Related

How to convert mysql query to JPQL query

I have working MySQL query:
SELECT date(timestamp), hour(timestamp), sum(numberDet), count(*)
FROM human_det_counter
GROUP BY hour( timestamp ) , day( timestamp )
ORDER BY date(timestamp)
I want select records, group by hour from each day and sum their number of detections.
I have tried this query:
SELECT date(h.timestamp), hour(h.timestamp), sum(h.numberDet), count(h)
FROM HumanDetCounter h
GROUP BY hour( h.timestamp ) , day(h.timestamp )
ORDER BY date(h.timestamp)
,but it's not working. I readed that jpa don't support Hour() function and that I should use TO_CHART function, but I don't know how.
Okey, I figured it out. There's my query if someone needs that:
createQuery("SELECT cast(Date(h.timestamp) as string), substring(h.timestamp, 12,2) , sum(h.numberDet), count(h) FROM HumanDetCounter h GROUP by substring(h.timestamp, 12,2), substring(h.timestamp, 9,2) ORDER BY h.timestamp")
It's probably not the best solution, but it's work :)

How to sum time using mysql

I have to sum the time in a table. In my table there is duration field and the type is "time" .
I have tried this query it returns only seconds. but I need hours and minutes and seconds.
in my table there is two rows one row has 00:20:10 and another is 00:15:05
so I need to display as 00:35:15
SELECT SEC_TO_TIME( SUM( MINUTE( duration ) ) ) AS totaltime
FROM users
please try this one:
SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( `duration` ) ) ) AS totaltime
Convert it to seconds and then back to time
SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( duration ) ) ) AS totaltime

date_trunc PostgreSQL function equal for mySQL

Im trying to retrieve data to make statistics, im using mySQL and i cant get the following function to work - the postgreSQL is working.
I want to retrieve the request for the last month and count the amount of new requests for each day.
postgreSQL
SELECT count(*), date_trunc('day', created_at) as date FROM requests
WHERE(created_at > '2014-08-13 00:00:00') GROUP BY 2 ORDER BY 2 ASC;
*mySQL - my code *
SELECT count(EXTRACT(DAY FROM created_at)), EXTRACT(DAY FROM created_at) as date
FROM `requests`
WHERE EXTRACT(DAY FROM NOW() - INTERVAL 1 MONTH)
GROUP BY date
Final code
SELECT count( * ) , date( created_at ) AS date
FROM `requests`
WHERE DATE( created_at ) > DATE( DATE_SUB( NOW( ) , INTERVAL 1 MONTH ) )
GROUP BY date
The equivalent for your case is date():
select date(created_at), count(*)
from requests
. . .
This isn't a general replacement, but it works to remove the time portion of a date.
EDIT:
Perhaps the better solution for these two databases is:
select cast(created_at as date)
This is ANSI standard and works in both these databases (as well as SQL Server). I personally don't use this in general, lest I accidentally use it in Oracle, causing difficult to find errors. (dates in Oracle have a time component, alas.)

How can i get the average time from timestamp between rows in a subselect query?

This query returns the date first and the last call submitted to the database per day. Now i want to get the avrg duration between each call submitted in in that time period. but it always return null???
To make it very easy: the normal way to calculate this would be deviding the hours between these two times by the number of calls. How to do this in MYSQL?
Please help!
SELECT DATE_FORMAT(last_call, '%d') AS day, MIN(last_call) AS checkin,
MAX(last_call) AS checkout, TIMEDIFF(MAX(last_call), MIN(last_call)) AS work_time
,(SELECT b.last_call - a.last_call / COUNT(calls.call_id)
FROM calls AS a
JOIN calls AS b ON b.last_call = (a.last_call + 1)
) AS intv
FROM calls
GROUP BY EXTRACT( DAY FROM last_call )
For whom soever could need this:
SELECT TIME_TO_SEC(TIMEDIFF(MAX(cl1.last_call), MIN(cl1.last_call))) / COUNT(cl1.call_id)
FROM calls AS cl1
WHERE EXTRACT(DAY FROM cl1.last_call ) = EXTRACT(DAY FROM calls.last_call )
) AS 'intvl'
FROM calls
WHERE MONTH(last_call) = MONTH(NOW()) LIMIT 1

mysql get sum of hours / minutes / seconds

I have a table with: userid and timestamp each time a user opens a page a new field is inserted.
I am trying to get the total amount of hours / minutes / days / weeks that appear in a 1 month interval for multiple users.
I have tried a bunch of different queries but each have ended up terribly inefficient.
Ideally I'd like to end up with something like:
userid | minutes | hours | days | weeks
1 10080 168 7 1
2 1440 24 1 0
Hopefully someone can shed some light on how to do this.
Below is a query that I tried:
SELECT
w.time AS `week`,
d.time AS `day`,
h.time AS `hour`,
m.time AS `minutes`
FROM (
SELECT
SUM( t.time ) AS `time`
FROM (
SELECT
COUNT( DISTINCT WEEK( `timestamp` ) ) AS `time`
FROM table
WHERE
userid = "1"
AND
`timestamp` > DATE_SUB( NOW( ) , INTERVAL 1 MONTH )
GROUP BY MONTH( `timestamp` )
) t
) w,
(
SELECT
SUM( t.time ) AS `time`
FROM (
SELECT
COUNT( DISTINCT DAY( `timestamp` ) ) AS `time`
FROM table
WHERE
userid = "52"
AND
`timestamp` > DATE_SUB( NOW( ) , INTERVAL 1 MONTH )
GROUP BY MONTH( `timestamp` )
) t
) d,
(
SELECT
SUM( t.timestamp ) AS `time`
FROM (
SELECT
COUNT( DISTINCT HOUR( `timestamp` ) ) AS `time`
FROM table
WHERE
userid = "1"
AND
`timestamp` > DATE_SUB( NOW( ) , INTERVAL 1 MONTH )
GROUP BY DAY( `timestamp` )
) t
) h,
(
SELECT
SUM( t.timestamp ) AS `time`
FROM (
SELECT
COUNT( DISTINCT MINUTE( `timestamp` ) ) AS `time`
FROM table
WHERE
userid = "1"
AND
`timestamp` > DATE_SUB( NOW( ) , INTERVAL 1 MONTH )
GROUP BY HOUR( `timestamp` )
) t
) m
It seems awfully excessive for this task, maybe someone has something better?
It's not clear to me what you want to "total".
If you want to determine whether a user had a "hit" (or whatever transaction it is you are storing in the table) at any given minute within the month), and then you want to count the number of "minute periods" within a month that a user had a hit:
SELECT t.userid
, COUNT(DISTINCT DATE_FORMAT(t.timestamp,'%Y-%m-%d %H:%i')) AS minutes
, COUNT(DISTINCT DATE_FORMAT(t.timestamp,'%Y-%m-%d %H' )) AS hours
, COUNT(DISTINCT DATE_FORMAT(t.timestamp,'%Y-%m-%d' )) AS days
, COUNT(DISTINCT DATE_FORMAT(t.timestamp,'%X-%V' )) AS weeks
FROM mytable t
WHERE t.timestamp >= '2012-06-01'
AND t.timestamp < '2012=07-01'
GROUP BY t.userid
What this is doing is taking each timestamp, and putting it into a "bucket", by chopping off the seconds, chopping off the minutes, chopping off the time, etc.
Basically, we're taking a timestamp (e.g. '2012-07-25 23:15:30') and assigning it to
minute '2012-07-25 23:15'
hour '2012-07-25 23'
day '2012-07-25'
A timestamp of '2012-07-25 23:25:00' would get assigned to
minute '2012-07-25 23:25'
hour '2012-07-25 23'
day '2012-07-25'
Then we go through and count the number of distinct buckets we assigned a timestamp to. If that's all the hits for this user in the month, the query would return a 2 for minutes, and a 1 for all other period counts.
For a user with a single hit within the month, all the counts for that user will be a 1.
For a user that has all their "hits" within exactly the same minute, the query will again return a 1 for all the counts.
(For a user with no "hits" within a month, no row will be returned. (You'd need to join another row source to get a list of users, if you wanted to return zero counts.)
For a user with a "hit" every second within a single day, this query will return counts like that shown for userid 2 in your example.
This result set gives you a kind of an indication of a user's activity for a month... how many "minute periods" within a month the user was active.
The largest value that could be returned for "days" would be the number of days in the month. The largest possible value to be returned for "hours" would be 24 times the number of days in the month times. The largest possible value returned for "minutes" would be 1440 times the number of days in the month.
But again, it's not entirely clear to me what result set you want to return. But this seems like a much more reasonable result set than the one from the previously "selected" answer.
SELECT userid, SUM(MINUTE(timestamp)) AS minutes, SUM(MINUTE(timestamp))/60 AS hours, SUM(MINUTE(timestamp))/(60*24) AS days, SUM(MINUTE(timestamp))/(60*24*7) AS weeks
FROM Table
GROUP BY userid
If neccesary, use ROUND(SUM(MINUTE(timestamp)), 0) if you want integer numbers.