Corey
Corey

Reputation: 845

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?

Upvotes: 1

Views: 8081

Answers (2)

spencer7593
spencer7593

Reputation: 108500

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.

Upvotes: 1

Carlos Vergara
Carlos Vergara

Reputation: 3622

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.

Upvotes: 1

Related Questions