How to sum time using mysql - 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

Related

How to calculate times in SQL

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

Why am i getting this error message ( #1111 - Invalid use of group function )?

I am trying to retrieve the TIMEDIFF from the last_call field of every row, where TIMEDIFF between current row and next row is greather than 10 min!
Can someone please help? Meybe there is a better way of doing this?
My goal is to get the total of all timediff between severall database entrys but only if they are greather than 10 min.
SELECT DATE_FORMAT( last_call, '%d' ) AS 'day',
(
SELECT TIME_TO_SEC(TIMEDIFF(MAX(cl1.last_call), MIN(cl1.last_call)))
FROM calls AS cl1
WHERE TIME_TO_SEC(TIMEDIFF(MAX(cl1.last_call), MIN(cl1.last_call))) > 600
AND cl1.calling_agent=9
AND EXTRACT(DAY FROM cl1.last_call ) = EXTRACT(DAY FROM calls.last_call )
) AS 'brake'
FROM calls
WHERE calling_agent =9
AND last_call > DATE_SUB( now( ) , INTERVAL 12 MONTH )
GROUP BY EXTRACT( DAY FROM last_call )

MySQL Distinct Active Users for the last month query

Alright I have tried alot and this looks just about right for me , but its def not:
SELECT COUNT( DISTINCT 'uid' ) AS `Records` , DATE( FROM_UNIXTIME( `epoch_timestamp` ) ) AS `Date`
FROM `log`
GROUP BY DATE( FROM_UNIXTIME( `epoch_timestamp` ) )
LIMIT 0 , 30
For w.e reason it returns a 1 next to each date. If I take out the distinct it appears to give a total records for that day count.
Seems like your sql is incorrect, try replacing the single quotation marks around 'uid' with `.
SELECT COUNT( DISTINCT `uid` ) AS `Records` , DATE( FROM_UNIXTIME( `epoch_timestamp` ) ) AS `Date`
FROM `log`
GROUP BY DATE( FROM_UNIXTIME( `epoch_timestamp` ) )
LIMIT 0 , 30

Joining two mysql queries containing time functions

I need to join the result of the next two queries:
SELECT EXTRACT( HOUR
FROM (
TIMEDIFF( NOW( ) , (
SELECT MIN( date_cmd )
FROM arc_commande_temp
WHERE id_cmd = '6580' ) )
)
) AS HOURS
SELECT EXTRACT( MINUTE
FROM (
TIMEDIFF( NOW( ) , (
SELECT MIN( date_cmd )
FROM arc_commande_temp
WHERE id_cmd = '6580' ) )
)
) AS MINS
I know I have to use join them somehow but this Extract functions are giving me lots of problems. I have the next query that still works
SELECT
*
FROM
(SELECT
MIN(date_cmd) AS HOURS
FROM
arc_commande_temp
WHERE
id_cmd = '6580') AS HOURS,
(SELECT
MIN(date_cmd) AS MINS
FROM
arc_commande_temp
WHERE
id_cmd = '6580') AS MINS
it's not what I need but I'm trying to modify it to get what I want - but when I try to change something else in order to build the query I need... I get an error :-(
Thanks in advance and regards
SELECT
EXTRACT(HOUR FROM (TIMEDIFF(NOW( ), (
SELECT MIN(date_cmd)
FROM arc_commande_temp
WHERE id_cmd = '6580'
)))) AS HOURS
,
EXTRACT(MINUTE FROM (TIMEDIFF(NOW( ) , (
SELECT MIN( date_cmd )
FROM arc_commande_temp
WHERE id_cmd = '6580'
)))) AS MINS

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.