A
A
Alceo2020-05-07 01:11:59
MySQL
Alceo, 2020-05-07 01:11:59

How to calculate the time between records-events in the database?

Hello everyone,

help me write a sql query and python code.

There is a mysql database that stores events like 1, 2 (fell asleep, woke up)

id - dt - event
9 - 2020-05-07 00:26:40.000000 - 1
10 - 2020- 05-07 00:30:02.000000 - 2

Help me write a script that will calculate sleep time per day / week / month?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alceo, 2020-05-11
@Alceo

https://stackoverflow.com/questions/18272625/calcu...

S
Sergey Pankov, 2020-05-07
@trapwalker

If you cannot fall asleep twice in a row without waking up, or, conversely, wake up twice without falling asleep, then window functions will help you. Here , I specifically looked, they are in MySQL.
You need to filter out only alternating falling asleep / waking up from events, the window size should cover two neighboring records, if the first one is "falling asleep", then this will be a sleep period, if "waking up", then a wakefulness period.
It seems to be exactly what you need.
How to make a request from python you will find in every first example found through Google .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question