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 8217891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:36:02+00:00 2026-06-07T12:36:02+00:00

I have a MySQL table like this: CREATE TABLE IF NOT EXISTS `mytable` (

  • 0

I have a MySQL table like this:

CREATE TABLE IF NOT EXISTS `mytable` (
  `ts` datetime NOT NULL,
  `cnt` int(10) unsigned NOT NULL,
  PRIMARY KEY (`ts`)
) ENGINE=InnoDB;

where we store the value of an event counter whenever we get an update; those updates arrive at arbitrary time.

How could I extract the number of events for every X amount of time (eg. 5 minutes, a day, a month, etc)? I could simplify this to intervals easily extracted via date/time sql functions (eg. hour, day, month, etc…).

While we don’t have any guarantee, the average data is “dense” when compared to the intervals I’d like to extract. EG. data usually comes in multiple times every hour but I’ll never ask for the count of events in an interval < 1 hour. If there is a “problem” (eg. big holes) in the stored data, it is acceptable to have a “problem” in the results.

As an example, I could get the counter values I’m interested in with a query like this (24h period example):

SELECT ts, cnt
FROM mytable
GROUP BY DATE( ts ) 
ORDER BY ts DESC

…and the events count could easily be calculated by subtracting each row’s counter with its predecessor. But I’d like to do that in SQL, if possible.

Also, if there is a good name for this problem (I think it’s a rather common one when you work with time series and counters) I’d like to know it to improve my vocabulary 🙂

  • 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-07T12:36:04+00:00Added an answer on June 7, 2026 at 12:36 pm

    If you would add an AUTO_INCREMENT PRIMARY KEY onto your table, that would be aolveable.

    The table schema as you have presented it is invalid (PRIMARY KEY on timestamp, but no such column).

    Would you mind if we:

    ALTER TABLE mytable DROP PRIMARY KEY, ADD COLUMN id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY
    

    ?

    If so, then the following could be done, I’ll present in steps:

    SELECT
      TIMESTAMPDIFF(SECOND, m1.ts, m2.ts) AS diff_seconds,
      m2.cnt - m1.cnt AS diff_cnt
    FROM
      mytable m1 JOIN mytable m2 ON (m1.id = m2.id-1)
    ;
    

    The above shows the difference in time and in cnt between consecutive samples. Let’s add a third column:

    SELECT
      TIMESTAMPDIFF(SECOND, m1.ts, m2.ts) AS diff_seconds,
      m2.cnt - m1.cnt AS diff_cnt,
      (m2.cnt - m1.cnt)/TIMESTAMPDIFF(SECOND, m1.ts, m2.ts) AS cnt_per_second
    FROM
      mytable m1 JOIN mytable m2 ON (m1.id = m2.id-1)
    ;
    

    I evaluated cnt_per_second. Multiply by 60 to get cnt per minute, and so on.

    Now, the total average would be:

    SELECT 
      avg(cnt_per_second)
    FROM (
      SELECT
        TIMESTAMPDIFF(SECOND, m1.ts, m2.ts) AS diff_seconds,
        m2.cnt - m1.cnt AS diff_cnt,
        (m2.cnt - m1.cnt)/TIMESTAMPDIFF(SECOND, m1.ts, m2.ts) AS cnt_per_second
      FROM
        mytable m1 JOIN mytable m2 ON (m1.id = m2.id-1)
    ) sel_diff
    ;
    

    Add original n1.ts to first query if you want to know when a diff was recorded, and so you will also be able to know the average count events in a given time period.

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

Sidebar

Related Questions

I have a MySQL table that looks like this: `id` int(10) unsigned NOT NULL
I have a mysql table like this (sql): CREATE TABLE IF NOT EXISTS silver_and_pgm
I have a MySQL table like this CREATE TABLE IF NOT EXISTS `vals` (
I have a table like create table adjacencies( relationId int not null primary key
I have a table like create table adjacencies( relationId int not null primary key
I have a table that looks like this CREATE TABLE IF NOT EXISTS `language`
I have a table like this: CREATE TABLE IF NOT EXISTS `session` ( `id`
I have the following MySQL tables: CREATE TABLE IF NOT EXISTS `conversations` ( `id`
I have a MySQL table like this: Payments +----+---------------------+---------+ | id | date (sorted
I have a MySQL table looking like this: > describe books; +---------------+--------------+------+-----+---------+----------------+ | Field

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.