Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8293633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:53:43+00:00 2026-06-08T13:53:43+00:00

I have a table with: userid and timestamp each time a user opens a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T13:53:45+00:00Added an answer on June 8, 2026 at 1:53 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table users with primary index userId . Currently, each user has
Say I have a 'user_log' table with the following field: id user_id status_text timestamp
I have the following table UserID | User knows -------------------- a | b a
I have a table that stores user information. The table has a userid (identity)
I have a table that has a user id & timestamp, like the following.
I have a database table (called Info) that stores [Data], [fkID], [UserID], [Timestamp]. [Data]
I have a table like this: ID Type UserID Timestamp 1 1 1000 1312389223
Have a table called user_products that contains a record for each product the user
why select 'aaa' =0 return 1 (TRUE) if i have a table like userid
I have set up 2 two tables - table userid and table data in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.