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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:14:29+00:00 2026-05-23T16:14:29+00:00

i have a database with workers, stations and session. A session describes at which

  • 0

i have a database with workers, stations and session. A session describes at which time which worker has been on which station. I managed to build a query that gives me the duration of the overlap of each session.

SELECT 
        sA.station_id,
        sA.worker_id AS worker1, 
        sB.worker_id AS worker2, 
        SEC_TO_TIME(
            TIME_TO_SEC(LEAST(sA.end,sB.end)) - TIME_TO_SEC(GREATEST(sA.start,sB.start))
        ) AS overlap
    FROM 
        `sessions` AS sA, 
        `sessions` AS sB
    WHERE 
            sA.station_id = sb.station_id
        AND 
            sA.station_id = 6
        AND (
                sA.start BETWEEN sB.start AND sB.end
            OR 
                sA.end BETWEEN sB.start AND sB.end
        )

With this query i get an result like this

station_id  worker1     worker2     overlap
6             1               1     09:00:00
6             2               1     02:30:00
6             5               1     00:00:00
6             1               1     09:00:00
6             2               1     01:30:00
6             3               1     09:00:00
...
6             12              3     02:00:00
6             14              3     01:00:00
6             17              3     02:00:00
...

What i would like now is to sum up the overlap for every combination of worker1 and worker2 to get the overall overlap duration.

I tried different ways of using SUM() and GROUP BY but i never got the wanted result.

SELECT 
...
SEC_TO_TIME(
    **SUM**(TIME_TO_SEC(LEAST(sA.end,sB.end)) - TIME_TO_SEC(GREATEST(sA.start,sB.start)))
) AS overlap
...

#has as result
station_id  worker1 worker2 overlap
6   1   1   838:59:59

#in combination with
GROUP BY
    worker1

#i get
station_id  worker1 worker2 overlap
6   1   1   532:30:00
6   2   1   -33:00:00
6   3   1   270:30:00
6   5   1   598:30:00
6   6   1   542:00:00
6   7   1   508:00:00
6   8   5   53:00:00
6   9   1   54:30:00
6   10  1   310:00:00
6   11  1   -108:00:00
6   12  1   593:30:00
6   14  1   97:30:00
6   15  1   -53:30:00
6   17  1   293:30:00

the last result is close but i am still missing a lot of combinations. I also dont understand why the combination 8 – 5 is displayed.

thanks for ur help (and time to read)

  • 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-05-23T16:14:29+00:00Added an answer on May 23, 2026 at 4:14 pm

    aaargh, sorry for my stupidity, the solution was fairly simple

    ....
    SUM(((UNIX_TIMESTAMP(LEAST(sA.end,sB.end))-UNIX_TIMESTAMP(GREATEST(sA.start,sB.start)))/3600))
    ...
    GROUP BY station_id, worker1, worker2            
    ORDER BY worker1, worker2
    

    i switched to using timestamps and transforming it to hours by /3600 because my former used approach with TIME_TO_SEC and SEC_TO_TIME only used the TIME part of the DATETIME field and thereby produced some wrong numbers. With MySQL 5.5 i could use TO_SECONDS but unfortunately my server is still runing 5.1.

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

Sidebar

Related Questions

I have noticed that as the database of my application has grown, the time
BACKGROUND: Co-worker Adam has been using Google refine to process database downloads with much
I have a background worker running a long database task. i want to show
I have Database with date field. I see the time like: 1900-01-01 13:38:00.000 How
We have a database server application which consists of the followings components: 1) A
I have a background worker that performs loading of data from the database into
I have database with many tables. In the first table, I have a field
I have database application, I want to allow the user to restore the deleted
i have database table like this +-------+--------------+----------+ | id | ip | date |
Currently I have database with the following associations: One Client to Many Intakes One

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.