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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:58:36+00:00 2026-06-17T18:58:36+00:00

I have a Database with the following structure: rowid ID startTimestamp endTimestamp subject 1

  • 0

I have a Database with the following structure:

rowid       ID                  startTimestamp   endTimestamp   subject
1           00:50:c2:63:10:1a   ...1000          ...1090        entrance
2           00:50:c2:63:10:1a   ...1100          ...1270        entrance
3           00:50:c2:63:10:1a   ...1300          ...1310        door1
4           00:50:c2:63:10:1a   ...1370          ...1400        entrance
.
.
.

With this SQL-Query i can get the average differences between the endTime and the startTime between one row and the following row, sorted by subject and ID, with their min,max,variance and standard deviation:

SELECT ID,AVG(diff) AS average,
   AVG(diff*diff) - AVG(diff)*AVG(diff) AS variance,
   SQRT(AVG(diff*diff) - AVG(diff)*AVG(diff)) AS stdev,
   MIN(diff) AS minTime,
   MAX(diff) AS maxTime

FROM
(SELECT t1.id, t1.endTimestamp,
        min(t2.startTimeStamp) - t1.endTimestamp AS diff
FROM table1 t1
INNER JOIN table1 t2
ON t2.ID = t1.ID AND t2.subject = t1.subject
AND t2.startTimestamp > t1.startTimestamp  -- consider only later startTimestamps
WHERE t1.subject = 'entrance'
GROUP BY t1.id, t1.endTimestamp) AS diffs
GROUP BY ID

This works fine, if i have only a few rows on the same day with less time differences, you can see it in this sqlfiddle:

http://sqlfiddle.com/#!2/6de73/1

But when i have additional data on just a different day, i get bad values:

http://sqlfiddle.com/#!2/920b6/1

Therefore, I want to calculate the avg, min, max, variance, standard deviation for each single day.

I know that there are the DATE-Functions of MySQL but i couldn’t get it done…can somebody help me? Or do i have to write a piece of PHP code that could handle this?

  • 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-17T18:58:37+00:00Added an answer on June 17, 2026 at 6:58 pm

    Is it as easy as adding the date into the group by. Here is syntax that should work in both MySQL and SQLite, basing the date on the end time and assuming the end time is stored as a datetime:

    SELECT ID, thedate, AVG(diff) AS average,
       AVG(diff*diff) - AVG(diff)*AVG(diff) AS variance,
       SQRT(AVG(diff*diff) - AVG(diff)*AVG(diff)) AS stdev,
       MIN(diff) AS minTime,
       MAX(diff) AS maxTime
    FROM (SELECT t1.id, t1.endTimestamp, DATE(endtimestamp) as thedate,
                 min(t2.startTimeStamp) - t1.endTimestamp AS diff
          FROM table1 t1 INNER JOIN
               table1 t2
               ON t2.ID = t1.ID AND t2.subject = t1.subject AND
                  t2.startTimestamp > t1.startTimestamp  -- consider only later startTimestamps
         WHERE t1.subject = 'entrance'
         GROUP BY t1.id, t1.endTimestamp
        ) AS diffs
    GROUP BY ID, thedate
    

    If stored as a time stamp, see Marty’s comment.

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

Sidebar

Related Questions

I have a SQLite Database with the following structure: rowid ID startTimestamp endTimestamp subject
I have the following database structure. ID | Name | Marks I am trying
I have two database tables with the following structure: actions: action_id int(11) primary key
I have no control over database schema and have the following (simplified) table structure:
I have the following database structure: Event table Id - Guid (PK) Name -
I have a database (running on postgres, precisely) , with the following structure :
I have a three level database with the following structure (simplified to only show
I have some info saved in a database field with the following structure: First
I have a database with the following structure: CREATE TABLE entity ( id SERIAL,
I have a database with the following structure: id msg 1 'Hello' 2 'Bye'

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.