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

  • SEARCH
  • Home
  • 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 8146015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:51:10+00:00 2026-06-06T13:51:10+00:00

I have a very simple MySQL table Name | Time | Count James |

  • 0

I have a very simple MySQL table

Name  |  Time  |  Count
James |  11:00 |  10
Simon |  11:00 |  5
James |  11:30 |  4
Oliver|  11:30 |  2
James |  12:00 |  1
etc.

I would like to create a MySQL query which sums the Count for each person and outputs a last 1 hour value, a last 3 hour value, a last 6 hour value, a last 12 hour value and a last 24 hour value.

e.g. if the time is now 12:59 the above table would output:

Name  |  Last-1  |  Last-3  | Last-6
James |  1       |  15      | 15
Simon |  0       |  5       | 5
Oliver|  0       |  2       | 2

Is it possible to do this in a single query? So far I have:

SELECT name, HOUR( NOW( ) ) - HOUR( time ) AS lasthours, SUM( count ) AS c
FROM table
GROUP BY name, lasthours
HAVING lasthours =0
ORDER BY c DESC

Which gives me the past hour, but how do I get the additional columns?

Any help much appreciated.

EDIT:

The accepted answer needed tweaking slightly and came out as follows:

SELECT  name , 
        SUM( IF( HOUR( TIMEDIFF( NOW( ) ,  time ) ) <=0,  count , 0 ) ) AS  `Last hour` ,
        SUM( IF( HOUR( TIMEDIFF( NOW( ) ,  time ) ) <=3,  count , 0 ) ) AS  `Last 3 hours` ,
        SUM( IF( HOUR( TIMEDIFF( NOW( ) ,  time ) ) <=6,  count , 0 ) ) AS  `Last 6 hours` ,
        SUM( IF( HOUR( TIMEDIFF( NOW( ) ,  time ) ) <=12,  count , 0 ) ) AS  `Last 12 hours` ,
        SUM( IF( HOUR( TIMEDIFF( NOW( ) ,  time ) ) <=24,  count , 0 ) ) AS  `Last 24 hours` ,
        SUM( IF( HOUR( TIMEDIFF( NOW( ) ,  time ) ) <=48,  count , 0 ) ) AS  `Last 2 days` ,
        SUM( IF( HOUR( TIMEDIFF( NOW( ) ,  time ) ) <=72,  count , 0 ) ) AS  `Last 3 days` ,
        SUM( IF( HOUR( TIMEDIFF( NOW( ) ,  time ) ) <=96,  count , 0 ) ) AS  `Last 4 days` ,
        SUM( IF( HOUR( TIMEDIFF( NOW( ) ,  time ) ) <=120,  count , 0 ) ) AS  `Last 5 days` ,
        SUM( IF( HOUR( TIMEDIFF( NOW( ) ,  time ) ) <=144,  count , 0 ) ) AS  `Last 6 days` ,
        SUM( IF( HOUR( TIMEDIFF( NOW( ) ,  time ) ) <=168,  count , 0 ) ) AS  `Last week`
FROM  table
GROUP BY  name
ORDER BY `Last hour` DESC
  • 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-06T13:51:11+00:00Added an answer on June 6, 2026 at 1:51 pm
    SELECT   `name`,
             SUM(IF(HOUR(TIMEDIFF(CURTIME(), `Time`)) <= 1, `Count`, 0)) AS `Last-1`,
             SUM(IF(HOUR(TIMEDIFF(CURTIME(), `Time`)) <= 3, `Count`, 0)) AS `Last-3`,
             SUM(IF(HOUR(TIMEDIFF(CURTIME(), `Time`)) <= 6, `Count`, 0)) AS `Last-6`
    FROM     `table`
    GROUP BY `name`
    

    See it on sqlfiddle.

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

Sidebar

Related Questions

I currently have a very simple MySQL database (articlesDB) with 1 table (articles) and
I have a MySQL database that has a few very simple tables. I would
I would like to create a very simple PHP page for a site, which
I have a very simple chat system I've built using PHP and MySQL (this
I have a simple referrers table with three columns: Referrer //string name of partner
I have very strange problem with mySQL and simple query with simple index. I
I have a very simple table structure here. Just a list of words related
So. I have a table of data, in a MySQL database, eg: Artist Name
I have a very simple table that contains a list of 'victims' and the
Let's assume we have this very simple 'test' table. There is: 5 bob, 2

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.